Imagine you have an Excel spreadsheet with two buttons named populate-current
and populate-all
. Both buttons execute the same Office Script function that looks something like this:
function populateByRowIndex(workbook: ExcelScript.Workbook, rowIndex: number): void {
// code to fill row values
}
function getAllRowIndexesToPopulate(workbook: ExcelScript.Workbook): number[] {
// retrieve row indexes
}
function main(workbook: ExcelScript.Workbook) {
const IS_POPULATE_ALL_BUTTON = ???
if (IS_POPULATE_ALL_BUTTON) {
for (const rowIndex of getAllRowIndexesToPopulate(workbook)) {
populateByRowIndex(workbook, rowIndex)
}
} else {
populateByRowIdx(workbook, workbook.getActiveCell().getRowIndex())
}
}
What value should be assigned to IS_POPULATE_ALL_BUTTON
in order to determine which button was clicked and run the appropriate function?
Or is there a way to reuse the logic from one script into another without duplicating code through some method other than copy-pasting? Using import { ... } from ...
gives an error "Cannot use import statement outside a module," and const { ... } = require(...)
gives "Cannot find name 'require'." Essentially, I am searching for a solution to avoid repeating code by sharing the logic from populateByRowIndex
, even if it requires using one or more script files.