The following Google Apps Script is a slight variation of my post on bulk creating Google Docs from Google Sheet data.
This time I was looking to work with Sheet data where students would get a mark for each module they completed, but not all students completed the same number of modules. I wanted to translate this information into a table in a Doc, one row per module, and avoid empty rows.
So I ultimately went from a table with a header row and one more row for the first module mark (because everybody completed at least one), to having the Apps Script check for any further module results and then appending a new table row to add them.
The Code
As there is an original blog post about this tool, I am just going to focus on the specific code for the table appending.
We get all of the tables in the Google Doc, check at least one exists, and then I have assumed you are working with the first table for this:
var tables = newFileBody.getTables();// checks there is a table before proceedingif (tables.length > 0) {// grabs the first tablevar moduleTable = tables[0];
Each module has a code and result, so we check neither is blank. If they are not, we can append a new table row and add the values in:
if (module2code && module2mark !== '') {var row2 = moduleTable.appendTableRow();row2.appendTableCell(module2code.toString());row2.appendTableCell(module2mark.toString());}
We can then simply repeat the above apps script for the other module code/mark columns.
Download
Append Sheet data to a Doc with varying Table sizes folder (please use 'File' > 'Make a copy' for your own version).

No comments:
Post a Comment