Last active
March 29, 2017 09:39
-
-
Save zlatko-michailov/2b0418c986d9da6ee0bdf7aa346d3a4f to your computer and use it in GitHub Desktop.
Excel JavaScript API Niceties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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