How to place a dynamic table in openXml wordprocessing document? - openxml-sdk

Using an OpenXml document, there are content controls where the data gets added based on the data from the database for that record; but now? Need to add a dynamic Table where it could have from 1 to 10 rows. always has 5 columns.
Getting the data to put in is easy, they will be driven by variables. so they can be named Row 1 column 1, col 2, col3, col4 and col5;
I can make variables to say useRow1 (to 10) = true/false; these are the #using defines:
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using OpenXmlPowerTools.Commands;
using OpenXmlPowerTools;
each set of table rows will depend on which record it is. The choices are to add rows or just make a static table with 10 rows where the bottom ones are blank?

You can simply create a fonction that return your dynamic Table.
The fonction build the table depending your datas, and you only add the needed rows.
public Table CreateTable(params type[] datas)
{
Table table = new Table();
foreach(Type line in par.rows)
{
Row row = new Row();
...
table.Append(row);
}
return table;
}

Related

Exceljs How to addRow and merge cells

So I need to generate an excel spreadsheet based on data from DB. Once I add in all the data from the DB I need to add in a new row at the bottom that says when the spreadsheet was generated like so:
I don't know how to get the cells to merge here, I can't use worksheet.mergeCells() since I don't know how many rows of data there will be in the DB. How would I merge the cells when using worksheet.addRow()?
I have come across a similar problem and have used something close to this:
const newRow = worksheet.addRow(["This report is..."]);
const currentRowIdx = worksheet.rowCount; // Find out how many rows are there currently
const endColumnIdx = worksheet.columnCount; // Find out how many columns are in the worksheet
// merge by start row, start column, end row, end column
worksheet.mergeCells(currentRowIdx, 1, currentRowIdx, endColumnIdx );

How to combine content from several tables into one?

this is the first time I'm posting here.
I'm trying to make a spreadsheet for my work on Google Sheets and I want to make a table with all the information from 5 other tables without having to copy and paste it.
I have 5 tables, all of them have the same number of columns and the same type of data in each column.
I need to make a new table that shows all the data from all the tables in one.
To make it simpler let's suppose I have 3 tables A, B and C
Each of the tables are like this:
Table A:
DATE|DESCRIPTION|PRICE
1/10|AAAAAAAAAAA|10000
2/10|BBBBBBBBBBB|20000
3/10|CCCCCCCCCCC|30000
Table B:
DATE|DESCRIPTION|PRICE
4/10|DDDDDDDDDDD|40000
5/10|EEEEEEEEEEE|50000
6/10|FFFFFFFFFFF|60000
Table C:
DATE|DESCRIPTION|PRICE
7/10|GGGGGGGGGGG|70000
8/10|HHHHHHHHHHH|80000
9/10|IIIIIIIIIII|90000
I would like to make a table D with the information from tables A, B and C like this:
Table D:
DATE|DESCRIPTION|PRICE
1/10|AAAAAAAAAAA|10000
2/10|BBBBBBBBBBB|20000
3/10|CCCCCCCCCCC|30000
4/10|DDDDDDDDDDD|40000
5/10|EEEEEEEEEEE|50000
6/10|FFFFFFFFFFF|60000
7/10|GGGGGGGGGGG|70000
8/10|HHHHHHHHHHH|80000
9/10|IIIIIIIIIII|90000
Also if I add one more row in any of the tables I want table D to update as well automatically either by adding the new row at the end or in another position... It doesnt really matter as long as it appears in table D.
How can I do this with a formula without having to copy and paste it?
Thank you so much!
Use query() with an { array expression }, like this:
=query(
{
Sheet1!A1:C;
Sheet2!A2:C;
Sheet3!A2:C
},
"where Col1 is not null",
1
)
You can use query for that
=QUERY({Sheet1!A2:C; Sheet2!A2:C; Sheet3!A2:C},"Select * where Col1 is not Null",0)
Please note while combining array, make sure to use similar range reference, that means row and column size of each array reference should be same otherwise it will throw Reference Error
Reference
Query

Query and its total row

I have a table generated by a query with data drawn from another sheet. At the end of of the table, I am adding a total-row.
When the rows generated by the query is more, total-row restricts the table. Is there any way to detect the number of rows generated by the query and insert more rows above the total-row?
I have the number of rows generated by the query displayed in a cell and row number of total-row in another cell. Can a macro be triggered when the first is more than the second?
Regards,
Pravin Kumar.
It is not possible to append the total row, since it depends on the query result and it will generate a redundancy problem. Any solution to that?
add it into your query in array {}. lets say your QUERY outputs 3 columns and 3rd column holds the sum:
={QUERY(A:C, "select A,B,sum(C) where A = 'xx' group by A,B label sum(C)''", 0);
"Total", "", QUERY(A:C, "select sum(C) where A = 'xx' label sum(C)''")}
or to add row between:

Remove rows based on duplicates in a single column in Google Sheets

I have spreadsheet similar to this:
I'd like to remove all duplicates of row based on the first column data.
So from this screenshot row, 1 and 2 would be kept, and row 2 would be removed. Any help would be greatly appreciated.
P.S. In my case I have columns from A to AU and rows from 2 to 9500.
Thank you.
Maya's answer and the solution AJPerez linked both work.
You can also use Filter View, which doesn't require deleting rows or creating new rows/sheets.
First create a helper column, say to the left of all your data. If your data starts on row 1, then create a blank row above all your data; if not, you are fine. Afterwards, on the first row where you have data, say row 2, write in the formula
=iserror(match(B2,B$1:B1,0))
Replace "2" with the row number of the first row of your data and "1" with that number minus 1. Also populate the rest of the column with the formula. (Unfortunately, arrayformula doesn't work here.) The formula outputs TRUE when the entry in B# has not occurred in cells above.
Note that this assumes your data now starts with column B and column B is where you want the filter to base. If that's not the case, just edit the column index too accordingly.
Select this new helper column. Go to Data -> Filter Views... -> Create a new Filter. Select filter by value and check TRUE only.
Caveat: this filter can only work if there are actually rows with TRUE value. This will always be the case as some entries are always unique.
If you want to avoid the caveat in your future applications, you can use filter by conditions with custom formula. The formula b2 should work.
But to begin with, even without the helper column, the above formula should work. Why doesn't it? That would be a good question for Google Support should it exist.
You can also use a google apps script to do this. To open the script editor from google sheets:
Choose the menu Tools > Script Editor.
Copy and paste the following script:
function removeDuplicates() {
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
var newData = [];
var ids = [];
for (var i in data) {
var row = data[i];
var duplicate = false;
if (ids.indexOf(row[0]) > -1) {
duplicate = true;
} else {
duplicate = false;
ids.push(row[0]);
}
if (!duplicate) {
newData.push(row);
}
}
sheet.clearContents();
sheet.getRange(1, 1, newData.length, newData[0].length).setValues(newData);
}
This assumes that you want to dedupe based on the contents of the first row of you sheet. If not you can adjust the row references from the 0 index to any other index you wish.
I would...
Create a new sheet
For column A, do =Unique(Sheet1!A:A)
Simply use VLOOKUP to populate the other columns. This will deliver the first value associated with the duplicates.

SpreadsheetApp insert a new column with header and formula

I need to insert a column after 5th column with a header(Average Clicks) also the values in this column must be calculated based on a formula (=E4*F4).
I can insert column after 5th by using this
sheet.insertColumns(5, 1);
Is it possible to create with header and also need to set the formula.
Thanks
You can use the ArrayFormula function to fill the formula into multiple rows.
function insertColumns() {
var sheet = SpreadsheetApp.getActiveSheet();
sheet.insertColumnAfter(6).getRange(1, 7).setValue("Average Clicks");
sheet.getRange(2,7).setFormula("=ArrayFormula(E2:E*F2:F)");
}

Resources