Aspose-Cells: How do I evaluate a formula? - aspose

Given the following code:
using Aspose.Cells
// {...}
Workbook workbook = new Workbook();
Worksheet virtualWorksheet = workbook.Worksheets[0];
virtualWorksheet.Cells[0, 0].Formula ="=1<2";
How do I evaluate the formula in Cell [0,0]?
bool isCellTrue = virtualWorksheet.Cells[0, 0].BoolValue; // will generate an exception at run-time

From: http://www.aspose.com/community/forums/permalink/212217/212232/showthread.aspx#212232
Call Workbook.CalculateFormula() method to calculate all the formulas in the workbook before getting/retrieving the calculated value at runtime, e.g.
Workbook workbook = new Workbook();
Worksheet virtualWorksheet = workbook.Worksheets[0];
virtualWorksheet.Cells[0, 0].Formula ="=1<2";
workbook.CalculateFormula();
bool isCellTrue = virtualWorksheet.Cells[0, 0].BoolValue;

Related

SpreadsheetFormats not working as expected

I am able to populate data from a query into a spreadsheet. However, I am having problems getting "ranged" formatting to work properly. The formatting for specific column (date) and row (header) work fine. But SpreadsheetFormatColumns, ...Rows, ...CellRange is not. I need to set the font and fontsize to the whole dataset.
Here is what I have tried.
<cfscript>
//Current directory path.
theFile = GetDirectoryFromPath(GetCurrentTemplatePath()) & "GridDump.xls";
//Create a new Excel spreadsheet object and add the query data.
theSheet = SpreadsheetNew("Raw Data");
FormatDate.dataformat = "dd-mmm-yy";
//Get Row Count and Row Range
RC = toString(result.recordcount+1);
RR = "1-" & RC;
//Get Column Count
CC = toString(ListLen(GridFieldNames));
//Get Column Letter
CL = chr(CC + 64);
//Get Column Range (Nummerical)
CRN = "1-" & CC;
//Get Column Range (Alphabetical)
CRA = "A-" & CL;
//Set Sheet Format
WholeSheet = StructNew();
WholeSheet.font="Consolas";
WholeSheet.fontsize=12;
//Set header Row Format
HeadRow = StructNew();
HeadRow.bold="true";
//Insert the Header Row
SpreadsheetAddRow(theSheet,GridFieldNames);
//Insert the Data
SpreadsheetAddRows(theSheet,result);
//Format the Data
SpreadsheetFormatCellRange(theSheet,WholeSheet,1,1,RC,CC);
//SpreadsheetFormatRows(theSheet,WholeSheet,RR);
//SpreadsheetFormatColumns(theSheet,WholeSheet,CRN);
SpreadsheetFormatRow(theSheet,HeadRow,1);
//Header Row
SpreadsheetFormatColumn(theSheet,FormatDate,1);//Date Column
SpreadsheetAddFreezePane(theSheet,0,1);//Top Row Only
//SpreadSheetAddAutofilter(theSheet,"A1:J1");
</cfscript>
Here are the results
I'm getting the same result for all three of the "ranged" formatting functions. The format stops part way through the spreadsheet. I expect the whole dataset to accept any of the ranged function formats.
I got the same result with CF 2018,0,04,314546. Could just be a limitation of XLS format.
Switching to XLSX worked fine for me:
theSheet = SpreadsheetNew("Raw Data", true);
YMMV, but what also worked with CF2018 was using SpreadsheetFormatColumns() instead of SpreadsheetFormatCellRange().

Google Apps Script Copy a range to another sheet based on the value of a cell

I have a table in a sheet in which I have a vlookup to check against another sheet which contains the same data and identify if there are any missing rows (discrepancy).
I want to copy and paste the missing rows identified with the vlookup formula: column I = "#N/A"
I am stuck with the following code, any help is greatly appreciated:
function CopyMissingRows(e) {
if (e.values[8] == '#N/A') {
// do something
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('A2:G2').activate();
var currentCell = spreadsheet.getCurrentCell();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Good events'), true);
spreadsheet.getRange('Sales cal').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
}
else {
// do something else
}

How can I get rid of this text:u and number: infront of my list?

import xlrd
datafile ="somefile"
workbook = xlrd.open_workbook(datafile)
sheet = workbook.sheet_by_index(0)
def get_cell_range(start_col, start_row, end_col, end_row):
return [sheet.row_slice(row, start_colx=start_col, end_colx=end_col+1) for row in xrange(start_row, end_row+1)]
print get_cell_range(0,13,2,17)
Output
[[text:u'CENTRAL', number:4.0, number:0.0], [text:u'COASTAL', number:1.0, number:1.0], [text:u'PIEDMONT', number:5.0, number:8.0], [text:u'STUDENT', number:3.0, number:4.0], [text:u'WEST', number:4.0, number:4.0]]
Sheet.row_slice() returns Cells. If you print them directly, it will contain type info. Try to get value by yourself.
cells = get_cell_range(0,13,2,17)
values = [item.value for item in cells]

T4 Templates : Reading resulting columns of a stored procedure table

I am learning T4 templates right now, and all examples I got on internet is about using the tables for code generation. I want to use stored procedure result columns to generate automated UI, is it possible? OR I have to create view for same query? in that case, how to read from view?
Thanks in advance.
I got the solution and here is how you can generate a rad grid directly from the sp name
<#
'requires: <## assembly name="System.Data" #>
dim Server as new Server(".\sqlexpress")
dim database as new Database(server, "xxxx")
dim strSpName as String= "sp_xxxx"
Dim dt as System.Data.DataTable= database.ExecuteWithResults("exec sp_GetEquipment").Tables(0)
dim ctlName as String = "grdEqp"
#>
<telerik:RadGrid ID="grd" runat="server" Skin="Web20" AutoGenerateColumns="false">
<MasterTableView>
<Columns>
<#
For Each column As System.Data.DataColumn In dt.Columns
#><telerik:GridBoundColumn DataField="<#=column.ColumnName #>" HeaderText="<#=column.ColumnName #>"/>
<#Next#>
</Columns>
</MasterTableView>
</telerik:RadGrid>
If you don't actually want to execute the stored procedure as various stored procedures have a number of different parameters passed then you could use the sp_describe_first_result_set system stored procedure to return the columns of the result set assuming there is just one.
/// <summary>
/// Returns table for which stored procedures need to be generated.
/// </summary>
string TableName = "usp_getNominalCode";
string SchemaName = "Financial";
DataTable DataTable
{
get
{
if (_table == null)
{
Server server = new Server(new ServerConnection(new SqlConnection(this.ConnectionString)));
SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder(this.ConnectionString);
Database database = new Database(server, connectionStringBuilder.InitialCatalog);
DataSet storedProcedureColumns = database.ExecuteWithResults("sp_describe_first_result_set #tsql= " + "'[" + SchemaName + "]" + ".[" + TableName + "]'");
_table = storedProcedureColumns.Tables[0];
}
return _table;
}
}
DataTable _table;
You can then query this table for it's structure like the other answer but it'll be a little more generic

How to disable automatic creation SeriesCollection in Excel Chart

I have already this code
Excel::_ApplicationPtr app("Excel.Application");
app->Visible[0] = false;
Excel::_WorkbookPtr book = app->Workbooks->Add();
Excel::_WorksheetPtr sheet = book->Worksheets->Item[1];
RangePtr pRange = sheet->Cells;
RangePtr pCell;
pCell = pRange->Item[1][1]; // A1
pCell->Value2 = "1";
pCell = pRange->Item[1][2]; // B1
pCell->Value2 = "1";
pCell = pRange->Item[1][3]; // C1
pCell->Value2 = "10";
pCell = pRange->Item[2][1]; // A2
pCell->Value2 = "3";
pCell = pRange->Item[2][2]; // B2
Cell->Value2 = "1";
pCell = pRange->Item[2][3]; // C2
pCell->Value2 = "20";
and next
Excel::RangePtr pBeginRange = pRange->Item[1][1];
Excel::RangePtr pEndRange = pRange->Item[5][9];
Excel::RangePtr pTotalRange = sheet->Range[(Excel::Range *)pBeginRange][(Excel::Range *)pEndRange];
_ChartPtr pChart2 = book->Charts->Add();
pChart2->ChartType = xlBubble3DEffect;
pChart2->SetSourceData((Excel::Range *)pTotalRange, (long)Excel::xlColumns);
How to disable automatic creation SeriesCollection in Excel Chart. I want to set the ranges manually. In the automatic creation all SeriesCollection has XValues in the first column. But I needn't it.
I've come across the same issue as well. From experimentation I think the following is happening:
When you create a new chart in VBA using the following code
Dim chSheet As Chart
Set chSheet = Charts.Add
Excel, trying to be helpful I bet, automatically looks at which ever worksheet your cursor had selected when you executed your code from the developer window and searches for the nearest dataset it thinks could be a range of values for the graph. This is then automatically populated in the graph. The only way around it I've found so far is to execute the following code to delete all series objects in the series collection object on the chart immediately after having created it. Touch counter intuitive but it works...
Public Sub DeleteChartSeries(chartSheet As Chart)
'Shorter but perhaps less clean way of writing the code compared to below
Do Until chartSheet.SeriesCollection.Count = 0
chartSheet.SeriesCollection(1).Delete
Loop
'With chartSheet
' Do Until .seriesCollection.Count = 0
' .seriesCollection(1).Delete
' Loop
'End With
End Sub
Just hit a workaround: First move the cursor somewhere outside the UsedRange, then create the Chart: Won't do auto-detection. Then move back.
In other words, the following code works for me (Excel 2007):
' Assume src As Range (proposed source data for the chart) in the ActiveSheet.
' put the cursor somewhere outside the UsedRange to avoid Excel's
' 'helpful' auto detection.
Dim ur As Range
Set ur = src.Worksheet.UsedRange
ur.Cells(1, ur.Columns.Count).Offset(0, 2).Select
Dim crt As Chart
Set crt = src.Worksheet.Shapes.AddChart().Chart
' ^ does NOT do auto-detection.
Probably can't prevent it being created, but you can delete it immediately after creating the chart, then create you own series as required.
In VBA:
Set Chrt = wb.Charts.Add()
Chrt.SeriesCollection(1).Delete