Skip to content

Instantly share code, notes, and snippets.

@leachdaniel
Last active November 3, 2017 03:30
Show Gist options
  • Save leachdaniel/43fa5a069e678166de1287256b758aa5 to your computer and use it in GitHub Desktop.
Save leachdaniel/43fa5a069e678166de1287256b758aa5 to your computer and use it in GitHub Desktop.
DocumentAppUtility.Tables.replaceContents.gs
var DocumentAppUtility = (function (dau, da) {
dau.Tables = (function(dautables, da) {
function replaceContents(table, data, deleteExtra, startRow) {
var numRows = table.getNumRows(),
startRow = (startRow || 0),
rowNum = null,
row = null,
cell = null;
for (var i=0; i < data.length; i++) {
rowNum = startRow + i;
if (numRows < i+startRow+1) {
row = table.appendTableRow(table.getRow(rowNum-1).copy());
}
else
{
row = table.getRow(rowNum);
}
for (var j=0; j<data[i].length; j++) {
if(row.getNumCells() < j+1) {
cell = row.appendTableCell(data[i][j]);
}
else {
cell = row.getCell(j);
if (cell.getText() !== data[i][j]) {
cell.setText(data[i][j]);
}
}
}
var numCells = row.getNumCells();
if (deleteExtra && numCells > data[i].length) {
var k = numCells-data[i].length,
cellNum = data[i].length;
while(k--) {
cell = row.getCell(cellNum++);
cell.setText("");
}
}
}
if (deleteExtra && numRows - startRow > data.length) {
var i = numRows-startRow-data.length,
rowNum = data.length+startRow;
while(i--) {
table.removeRow(rowNum);
}
}
}
dautables.replaceContents = replaceContents;
return dautables;
} (dau.Tables || {}, da));
return dau;
}(DocumentAppUtility || {}, DocumentApp));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment