Copying Sheet Data to Doc table

耗尽温柔 提交于 2021-01-29 08:05:59

问题


I have a sheet with information that needs to be filled and once a submit process is started it should copy the cell data to an existing table header in a document. There is only one table in the document with the header info.

I can get this to work just fine with appendTable, but I would like to append rows to the existing table in the document. Data from spreadsheet looks like [[1234, Jordan, Connecticut], [6899123, Job Site, Connecticut], [, , ]]

//  Sheet Data
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0]
var range = sheet.getRange(2,1,sheet.getLastRow(),sheet.getLastColumn());
var rows = sheet.getLastRow()
var values = range.getValues();


var document = DocumentApp.openById(tmpl);
var body = document.getBody();
   body.replaceText('#{jobNum}', job);
var table = body.getTables()[0]

document.saveAndClose()

回答1:


You already have the values in the variable values and you also selected the first table in the document in the variable table, now you just need to to loop through your Array and append rows using appendTableRow() and appendTableCell()

Your loop should look like this:

  for (var row = 0; row < values.length; row++) {
    var newRow = table.appendTableRow(); // Append new Row
    for (var col = 0; col < values[row].length; col++) {
      newRow.appendTableCell(values[row][col]); // Append a Cell with the value
    }
  }

This will append a row at the end of the table you already have in the document.



来源:https://stackoverflow.com/questions/38772013/copying-sheet-data-to-doc-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!