简体   繁体   中英

Sheet#copyTo(Spreadsheet) raises Service Error

I simply want to copy a template sheet and rename it when the script identifies that a sheet is missing.

8th line (with the copyTo ) isn't working. I get this error message:

Service error: Spreadsheets (line 202, file "statsContacts")

Similar questions on SO indicate an issue with Range#copyTo but that's a different method.

NB1: in finale version "communautes" and "feuillesExistantes" will be generated dynamically
NB2: I added the polyfill of Array.prototype.includes to be able to use it

function creerFeuillesManquantes(){
  var communautes = ['ESSAI', 'TEST', 'FOO'];
  var feuillesExistantes = ['Catégorie ESSAI', 'Catégorie TEST'];
  for (c in communautes){
    if (!feuillesExistantes.includes(communautes[c])){
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var template = ss.getSheetByName('TEMPLATE');  
      var newSheet = template.copyTo(ss);
      SpreadsheetApp.flush(); // avant de renommer, on s'assure que la copie soit faite
      newSheet.setName('Catégorie '+communautes[c]);
    }
  }
}

Try changing to the following:

function creerFeuillesManquantes(){
  var communautes = ['ESSAI', 'TEST', 'FOO'];
  var feuillesExistantes = ['Catégorie ESSAI', 'Catégorie TEST'];

  // move ss and template up to avoid making network calls every time
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var template = ss.getSheetByName('TEMPLATE');

  for (c in communautes){
    if (!feuillesExistantes.includes(communautes[c])){
      ss.insertSheet('Catégorie '+communautes[c], { template: template });
      SpreadsheetApp.flush(); // avant de renommer, on s'assure que la copie soit faite
    }
  }
}

I found that, when I switched to ss.insertSheet(...) instead of template.copyTo(...) , I received much better error messages, eg This action would increase the number of cells in the workbook above the limit of 5000000 cells. .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM