Every end of the month I duplicate a tab with multiple hidden rows, then delete all those hidden rows from the duplicate tab. For that purpose I modified the following code of Tanaike, the code running without any error, but all hidden rows still there.
Please advice what correction should be made for the code. Thanks in advance.
Here is the modified code:
function deleteHiddenRows() {
const spreadsheetId = SpreadsheetApp.openById(gid);
const sheetName = spreadsheetId.getSheetByName(tab);
const lastRow = sheetName.getLastRow();
const sheetId = sheetName.getSheetId();
const fields = "sheets(data(rowMetadata(hiddenByUser)),properties/sheetId)";
const rowMetadata = SheetsV4.Spreadsheets.get(gid, { fields: fields }).sheets[0].data[0].rowMetadata;
const requests = rowMetadata.reduce((ar, { hiddenByUser }, i) => {
if (hiddenByUser && i > 0 && i + 1 < lastRow) ar.push({ deleteDimension: { range: { sheetId, startIndex: i, endIndex: i + 1, dimension: "ROWS" } } });
return ar;
}, []);
SheetsV4.Spreadsheets.batchUpdate({requests}, gid);
}
New contributor
raf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.