How can I provide a calculated value that is based on two other values in an Aspose.Cells PivotTable? - excel

I am adding data fields to a PivotTable like so:
int TOTALQTY_COLUMN = 4;
int TOTALPRICE_COLUMN = 5;
. . .
pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Data, TOTALQTY_COLUMN);
pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Data, TOTALPRICE_COLUMN);
Or, providing more context:
Aspose.Cells.Pivot.PivotTableCollection pivotTables = pivotTableSheet.PivotTables;
int colcount = COLUMNS_IN_DATA_SHEET;
string lastColAsStr = ReportRunnerConstsAndUtils.GetExcelColumnName(colcount);
int rowcount = sourceDataSheet.Cells.Rows.Count;
string sourceDataArg = string.Format("sourceDataSheet!A1:{0}{1}", lastColAsStr, rowcount);
int index = pivotTableSheet.PivotTables.Add(sourceDataArg, "A7", "PivotTableSheet");
Aspose.Cells.Pivot.PivotTable pivotTable = pivotTables[index];
pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Row, DESCRIPTION_COLUMN);
pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Column, MONTHYR_COLUMN);
pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Data, TOTALQTY_COLUMN);
pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Data, TOTALPRICE_COLUMN);
I need another data field that is based on the values supplied to those two data fields (from the source data's TOTALQTY_COLUMN and TOTALPRICE_COLUMN columns), specifically TotalPrice divided by Total Qty.
Is this possible? If so, how can I do this?
I could create another column for the source data sheet that would contain this value and just reference it, but if I can avoid doing that by use of some calculation "trickery" it would be preferable.

I solved this by adding an extra column to my source data sheet, populating that by making the calculation described above (TotalPrice / TotalQty):
decimal avgPrice = 0.0M;
if ((TotalPrice > 0.0M) && (Quantity > 0))
{
avgPrice = TotalPrice/Quantity;
}
cell = sourceDataSheet.Cells[_lastRowAddedPivotTableData, 10];
cell.PutValue(Math.Round(avgPrice, 2));
...and then referencing that new data source column accordingly:
pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Data, AVGPRICE_COLUMN);

Related

export hashsmap to xlsx using nested for each loop

I have below loop trying to printout keys and values stored in a hashmap
// Create the first row corresponding to the header
Row header = Sheet.createRow(0);
int headerIndex = 0;
for (Map.Entry<String, List<String>> entry : resultsToPrint.entrySet()) {
String key = entry.getKey();
header.createCell(headerIndex).setCellValue(key);
int rowIndex = 1;
for (String value : entry.getValue()) {
// Creating row
Row row = Sheet.createRow(rowIndex);
// create cell
row.createCell(headerIndex).setCellValue(value);
rowIndex++;
}
headerIndex++;
}
the results I am getting looks like this:
This is what gets printed
what I need this loop to do, is to populate the values of the second column as well and if I add a third column, it needs to populate the values for the third column etc.
Please assist

How to get column and row count for a table in codede ui?

I want to get row and colun numbers for a table but can not do it.
here is my table
WinTable uIG1Table = this.UIProMANAGEWindow.UIMachineListWindow.UIG1Window.UIG1Table;
First I used methods I have seen in this link but it did not work
http://executeautomation.com/blog/storetableinlist/
int rowCount = uIG1Table.RowCount;
int colCount = uIG1Table.ColumnCount;
I searched for WinTable API and I tried to use API methods. Since there is no columng structures, I used ColumnHeaders for column counting , both row and column brings me 0. Whata should I do ?
int rowCount = uIG1Table.Rows.Count;
int colCount = uIG1Table.ColumnHeaders.Count;
MessageBox.Show(rowCount.ToString());
MessageBox.Show(colCount.ToString());
Try searching all rows on the window using following code example:
internal UITestControlCollection GetGridRows()
{
var winSearchResults = FindWindowSearchResults();// this can be any WinWindow object
WinRow gridRows = new WinRow(winSearchResults);
gridRows.SearchProperties.Add(WinRow.PropertyNames.Name, "Row ", PropertyExpressionOperator.Contains);// in case of my AUT, the rows name property had "Row " at start
gridRows.SearchConfigurations.Add(SearchConfiguration.AlwaysSearch);
return gridRows.FindMatchingControls();
}
Once you have a row object, you can get cell count through cells property as below:
rowFromGrid.Cells

match single cell value with column of values for every match return those rows Google-apps-script

I have a spreadsheet with 2 tabbed sheets. I am trying to run a macro so that when the user inputs a name in B2 of the 2nd sheet, it is matched with every instance of that name in the 1st sheet, column B. I then need to copy all of the data that appears in the matched cell's rows and have that pasted in the 2nd sheet starting with cell B3.
I have limited experience with VBA, but none with JS/Google-apps-script. Any help with how to write this would be greatly appreciated! Here is my first shot:
function onSearch() {
// raw data sheet
var original = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form Responses 2");
// search for student sheet
var filtered = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Student Progress Search");
// retrieving the values in the raw data array of names
var searchColumn = 2;
var lr = original.getLastRow();
var searchRange = original.getRange(2,searchColumn, lr, 1).getValues();
// retrieving the name submitted on search
var inputName = filtered.getRange(2, 2).getValue();
// loop through all the names in the raw data and identify any matches to the search name
for (var i = 0; i < lr; i++){
var dataValue = searchRange[i];
var r = dataValue.getRow();
var line = [[r]];
var paste = filtered.getRange(3, 3);
// if the data is a match, return the value of that cell in the searched sheet
if (dataValue == inputName){ return paste.setValues(line);
}
}
}
Not sure if the built-in QUERY function would work for you. This here does exactly what you are looking for:
=QUERY(Sheet1!B:B,"select B where LOWER(B) like LOWER('%" &B2& "%')")
For example, if a user enters 'joe', the function will match any entry containing 'joe', regardless of case.

How can I create a PivotTable using EPPlus to filter on the rows in one column and also a set of columns?

To create a PivotTable, source data needs to be provided from which to generate the PivotTable.
I have generated a "dummy" sheet for this purpose which contains raw data such as:
I need to generate a Pivot Table from that data so that it is generated like so:
IOW, it needs to have a filter on the "Description" column that allows the rows to be filtered (only show "Peppers" or whatever) AND a filter on which month columns to display (the sheet can have up to 13 month columns (Sep 15, Oct 15, etc.)) so that the user can choose 1..13 of those "month year" columns to display (technically, they could choose 0, but what would be the point)?
How can this be done? I've tried the following:
private void AddPivotTable()
{
var dataRange
rawDataWorksheet.Cells[rawDataWorksheet.Dimension.Address];
dataRange.AutoFitColumns();
var pivotTable
usagePivotWorksheet.PivotTables.Add(usagePivotWorksheet.Cells["A6"]
dataRange, "ProdUsagePivot");
pivotTable.MultipleFieldFilters = true;
pivotTable.GridDropZones = false;
pivotTable.Outline = false;
pivotTable.OutlineData = false;
pivotTable.ShowError = true;
pivotTable.ErrorCaption = "[error]";
pivotTable.ShowHeaders = true;
pivotTable.UseAutoFormatting = true;
pivotTable.ApplyWidthHeightFormats = true;
pivotTable.ShowDrill = true;
var descPageField = pivotTable.Fields["Description"];
pivotTable.PageFields.Add(descPageField);
descPageField.Sort
OfficeOpenXml.Table.PivotTable.eSortType.Ascending;
var descRowField = pivotTable.Fields["Description"];
pivotTable.RowFields.Add(descRowField);
. . . add others later
}
...but only get the filter for "Description" with no data beneath it:
What do I need to do yet to get the desired appearance/functionality?
Try this:
Create the entire PivotTable as usual and after adding all the required fields and calculations, move the DataPivotFields as RowFields.
In VBA will be something like this:
With PivotTable
.DataPivotField.Orientation = xlRowField
.Position = 2
End With
Also need to apply this: PivotTable.MergeLabels = True to have the Description cell merged for all the corresponding totals.
PD. Replace PivotTable with the PivotTable object in your code

Matlab, Create one cell of some cells

I have 3 cell arrays of some strings cell1, cell2 and cell3. I want to save them in one cell matrix.If the column size of each cell are col1 col2 and col3. I want to create one cell with size (max(col1,col2,col3)*3)).How can I?
cellmarix{:,1}=cell1;
cellmarix{:,2}=cell2;
cellmarix{:,3}=cell3;
but this created a cell of cells of size (1*3).
I also used
cellmatrix={cell1,cell2,cell3};
but the result was the same(1*3) cell of cells.
for example if I have
cell1={
'uzi'
'julian'
'ahyden'
'kwayne'
'riel'
'gazook'
'mustapha'
}
cell2={
'negro'
'kris'
'sascha'
'jimw'
'andi'
'andrei'
}
cell3={
'joncruz'
'youngsd'
'notzed'
'werner'
'cactus'
'Iain'
'faassen'
}
The result is :
cell_all={
'uzi' 'negro' 'joncruz'
'julian' 'kris' 'youngsd'
'ahyden' 'sascha' 'notzed'
'kwayne' jimw' 'werner'
'riel' 'andi' 'cactus'
'gazook' 'andrei' 'Iain'
'mustapha' [] 'faassen'
}
You can do the following:
cell_all = cell1;
cell_all(1:numel(cell2), 2) = cell2;
cell_all(1:numel(cell3), 3) = cell3;
If you have a lot of cells (like you say you do), you can resort to a loop:
n = 3; %// Number of columns
cell_all = cell1;
for k = 2:n
varname = sprintf('cell%d', k);
cell_all(1:numel(eval(varname)), 2) = eval(varname);
end
This is one of those rare cases where eval actually helps. However, I cannot help but wonder why there are so many cell variables in your workspace instead of populating one large cell array right from the start.
C = {'one','two','three'};
str = strjoin(C)
--> see:
TMW: join cell

Resources