Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

256
Visualizações
forEach function for multiple sheets in Google Scripts

I am attempting to run a script that will copy info from multiple different sheets to a different file. I am having issues with the forEach function. The error I am getting is:

Error
ReferenceError: sheets is not defined copy @ Code.gs:9 copyInfo @ Code.gs:4

function copyInfo() {
  var sheets = SpreadsheetApp.getActive().getSheets(); // get all sheets
  sheets.forEach(copy)
}


function copy() {
      var sheetName = sheets.getActiveSheet.getName();
      if(sheetName.includes("*"))
        var copySheet = SpreadsheetApp.getActiveSheet();
        var sheetName=copySheet.getName();
        var pasteSpreadsheet = SpreadsheetApp.openById("1NXmdq6yCKC6oiDAQNXcovj85697__rCeVJiIT_ou_gI");

  //Create new sheet if one doesn't exist
  var newSheet = pasteSpreadsheet.getSheetByName(sheetName);   
  
  if (newSheet != null) 
    pasteSpreadsheet.deleteSheet(newSheet);
    newSheet = pasteSpreadsheet.insertSheet();
    newSheet.setName(sheetName);

  //Copy and paste data
  
  // Clear the Google Sheet before copy
  newSheet.clear({contentsOnly: true});

  // get source range
  var source = copySheet.getRange("B2:B");
  var values = source.getValues();
  // get destination range
  var destination = newSheet.getRange(1,1,values.length,values[0].length);
   // copy values to destination range
  destination.setValues(values);
  }
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

This might work

function copyInfo() {
  const dontcopy = ["Sh1","Sh2",...];
  var sheets = SpreadsheetApp.getActive().getSheets(); // get all sheets
  sheets.filter(sh => !~dontcopy.indexOf(sh.getName()).forEach(s => {
    s.activate();
    copy();
  });
}
about 4 years ago · Juan Pablo Isaza Relatório

0

If you create a function to use with forEach, that function needs to accept an individual item as a parameter. In this case, each "sheet" from SpreadsheetApp.getActive().getSheets() will be passed as the first parameter into your copy function - but the copy function doesn't accept any params as is.

function copy(sheet) {
      var sheetName = sheet.getName();
      if(sheetName.includes("*"))
      ...
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda