Skip to content

Instantly share code, notes, and snippets.

@zlatko-michailov
Last active March 29, 2017 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zlatko-michailov/2b0418c986d9da6ee0bdf7aa346d3a4f to your computer and use it in GitHub Desktop.
Save zlatko-michailov/2b0418c986d9da6ee0bdf7aa346d3a4f to your computer and use it in GitHub Desktop.
Excel JavaScript API Niceties
function createAndPopulateTable(context, worksheetName, rangeAddress, hasHeaderRow, headerValues, bodyFormulas, tableCustomizer) {
var worksheet = context.workbook.worksheets.getItem(worksheetName);
// Calculate table-, body-, and header- ranges
var tableRange = worksheet.getRange(rangeAddress);
var bodyRange = tableRange;
if (hasHeaderRow) {
bodyRange = tableRange.getResizedRange(-1, 0).getOffsetRange(1, 0);
if (headerValues) {
// Set header values
var headerRange = tableRange.getRow(0);
headerRange.values = headerValues;
}
}
// Set body formulas
bodyRange.formulas = bodyFormulas;
return context.sync()
.then(function() {
// Create the table
var table = context.workbook.tables.add(tableRange, hasHeaderRow);
// Invoke the caller's customizer
if (tableCustomizer) {
tableCustomizer(table);
}
return context.sync();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment