Home > Enterprise >  Copies of Hidden Sheets in Apps Script
Copies of Hidden Sheets in Apps Script

Time:06-30

I have this fragment of code that make a copy of each Sheet in the document. I don't know if it makes copies of Hidden Sheets, the _temp copy of the hidden sheets do not appear in the interface but i suppose they are also hidden. In case it does the copies, how could I modify the code to not copy hidden sheets? I have a lot of hidden sh in my document and it could save time and avoid crashes.

var ss = SpreadsheetApp.openById(spreadsheetId);
var tempSheets = ss.getSheets().map(function(sheet) {
  var dstSheet = sheet.copyTo(ss).setName(sheet.getSheetName()   "_temp");
  var src = dstSheet.getDataRange();
  src.copyTo(src, {contentsOnly: true});
  return dstSheet;
});

Thanks

CodePudding user response:

This should work:

var ss = SpreadsheetApp.openById(spreadsheetId);
var tempSheets = ss.getSheets().filter(sheet => !.isSheetHidden()).map(function(sheet) {
  var dstSheet = sheet.copyTo(ss).setName(sheet.getSheetName()   "_temp");
  var src = dstSheet.getDataRange();
  src.copyTo(src, {contentsOnly: true});
  return dstSheet;
});

sheet.isSheetHidden() will return true if the sheet is hidden, and false otherwise. We first filter the list to non-hidden sheets, and then only copy the rest of the sheets.

CodePudding user response:

Copy unhidden sheets

function copyunhidden() {
  const ss = SpreadsheetApp.openById("id");
  ss.getSheets().filter(sh => !sh.isSheetHidden()).forEach(s => {
    s.copyTo(ss).setName(s.getName()   "_temp")
  });
}
  • Related