Forum Discussion
TonyEnglish
May 08, 2025Copper Contributor
Excel Script - not calculating as expected
Hi folks....I'm hoping somebody can shed a bit of light on what I'm doing wrong here. We have a simple holiday tracker - it has a small amount of VBA in it which I figured would be a good candidate ...
SergeiBaklan
May 09, 2025Diamond Contributor
You get Show/Hide values at very beginning, later you change MonthNumber, Show/Hide within grid are recalculated, but in array for show/hide are still old values. You need to get it from the grid again, or get just befor you will use it.
Not sure I understood entire logic, thus tried to reproduce as
function main(workbook: ExcelScript.Workbook) {
const sheetRefs = workbook.getWorksheet("Refs")
const rangeCurrentMonth = sheetRefs.getRange("CurrentMonth")
const sheetYear = workbook.getWorksheet("Year")
const rangeShowHide = sheetYear.getRange("ShowHide")
let MonthNumber: number = +rangeCurrentMonth.getValue()
MonthNumber > 1 ? MonthNumber -= 1 : ''
rangeCurrentMonth.setValue(MonthNumber)
sleep(0.1) // in general not necessary
const rangeShowHideValues = rangeShowHide.getValues()
const colCount: number = rangeShowHideValues[0].length
for( let col of Array.from(Array(colCount).keys() ) ) {
rangeShowHide
.getColumn(col)
.setColumnHidden( rangeShowHideValues[0][col] === "Show" ? false : true )
}
}
// Delay in seconds
function sleep(seconds: number) {
const waitUntil = new Date().getTime() + seconds * 1000
while (new Date().getTime() < waitUntil) { }
}
for the attached file. Not sure if scripts waits for the greed recalculation or not, added some delay. However, it works exactly the same without it.