Hi, i try to record office script to filter out or hide rows based on the cell value.
Its really should be an easy task to achieve yet im trying to sort it for 3rd day now.
I need to have script which is going to different tabs to check if there is "0" or "1" in column "T".
- If there is 0 -> hide the row
- If there is 1 -> unhide the row
It could be filtering out, grouping or whatever which will make it gone from the list, i cant use vba cause its online based file,
- the very same area will be filled in by user and the whole idea od the script would be to show only values for this week (column "t" checks if the data falls into range and set 0 if not and 1 if yes).
- If i use fixed filter it wont change when the date will set new 0s and 1s
- cant use filter formulas or power query because there is already plenty of tabs and i don't want to create more with only desired output.
i have column "T" containing formula resulting in 0 or 1, id like to run this daily and hide everything containing 0, and leave everything with 1 visible.
I recorded script and later used chatgpt to write the code:
function main(workbook: ExcelScript.Workbook) { let sheets = workbook.getWorksheets(); sheets.forEach(sheet => { let selectedSheet = workbook.getActiveWorksheet(); // Get the data from range T7:T5000 (headers) let rowData: string[] = selectedSheet.getRange("T:T").getValues()[0] ?? selectedSheet.getRange("T:T").getValues() .catch(error => { throw new Error("The range T7:T5000 is empty."); }); // Check if rowData is not empty if (rowData.length !== 0) { // Loop through the rowData to hide/show columns based on cell values for (let i = 0; i < rowData.length; i++) { if (rowData[i] === 0) { selectedSheet.getRangeByIndexes(0, i + 29, rowCount, 1).setRowHidden(true); } else if (rowData[i] === 1) { selectedSheet.getRangeByIndexes(0, i + 29, rowCount, 1).setRowHidden(false); } } } }); }
it results in error:
Line 7: Cannot read properties of null (reading '0')
Can't really understand how it can result in errors if i record this script with built in tools? (it doesn't matter if i record or ask any llm, the error is same or similar or says functions doesn't exist)
Thanks in advance