I have a script that creates a Google Doc based on a template with place holders and such, pulling the data from a Google Sheet that is the data source for an AppSheet app. The trigger for the script is an Appsheet action that calls the script and feeds the function parameters. Everything is working beautifully except... I have a "Sample" table that is pulling data to fill a table inside the Doc. Even this part works just fine when the template has enough rows in the table for the amount of sample rows needed for the job. However, I would like the final doc to have the right sized table. I cannot seem to get the script to either add rows as needed or delete empty rows when complete. Here is my latest attempt:
Solved! Go to Solution.
The way I do this sort of task is I have two template google docs. The one template is the main document. It has logos and artwork in the correct places. The second doc is a doc of tables. Each table has a header row and a content row with placeholders. The tables are styled because the tableRow.copy() method mostly (yeah mostly) carries over all formatting to the destination.
The workflow being that my data source specifies the tables it should be inserted into as well. So my flow is to make a copy of the main doctemplate and then flow down through my data and copying the required table from the tables doc, filling it using the method I showed and then grabbing the next table and repeating.
The tables doc is easy to understand as body.getTables() get's all the tables without messing about with elements and so on.
Hope that helps 😊
I have never used a Google App Script to interact with a Google Doc. I'd love to see the document you are starting with and the end result.
The error you are showing is implying that the function name of "deleteRow(i)" does not exist on the object of "table". Are you sure you are using the correct function name??
Actually, looking in the documentation...the correct function name is "removeRow" not "deleteRow". Refer to the link below for the available list of functions.
https://developers.google.com/apps-script/reference/document/table
My advice here is that instead of starting with a table that already has rows added, start with a table that has the header row and a secondary row with placeholders.
When you iterate through your data, you will (for each line of data) add a copy of the placeholder row and then replace the placeholders in that row. Note, you will not be globally looking through your document for placeholders, you will be checking that specific row only.
Do this for each item of data in your array. Finally, you can then remove the second row of your table that has the placeholders. Table.getRow(1).removeFromParent()
So maybe a little bit of code:
myTable = myDocBody.getTables()[0] // retrieves the first table in the document body
myPlaceholderRow = myTable.getRow(1)
Thanks @scott192! We ended up with a code that worked to remove the unnecessary rows of a oversized table in the template - I will play with your solution, as it seems cleaner. Appreciate the help!
The way I do this sort of task is I have two template google docs. The one template is the main document. It has logos and artwork in the correct places. The second doc is a doc of tables. Each table has a header row and a content row with placeholders. The tables are styled because the tableRow.copy() method mostly (yeah mostly) carries over all formatting to the destination.
The workflow being that my data source specifies the tables it should be inserted into as well. So my flow is to make a copy of the main doctemplate and then flow down through my data and copying the required table from the tables doc, filling it using the method I showed and then grabbing the next table and repeating.
The tables doc is easy to understand as body.getTables() get's all the tables without messing about with elements and so on.
Hope that helps 😊
User | Count |
---|---|
17 | |
14 | |
10 | |
7 | |
4 |