0
votes

Please anyone can help my? this is my code and throws We're sorry, a server error occurred. Please wait a bit and try again always. The idea is take a sheet generic and another sheet and copy all data from a sheet to generic sheet.

function importData(id){
  try{
    var sheet = SpreadsheetApp.openById(id);

    Logger.log("name of sheet: " + sheet.getName())

    var sheetToImport = sheet.getSheetByName("planilla para carga de datos");

    var sheetGeneric = DocsList.getFileById('0ArvYhlthIEV-dGxLOTI1MG5OSkI2SzRGRDhlendwcnc');

    var newSheet = sheetGeneric.makeCopy(sheet.getName()+" V2");

    var row = sheetToImport.getRange('A:Z');

    var range = row.getA1Notation();

    var values = row.getValues();

    var ss = SpreadsheetApp.openById(newSheet.getId()).getSheetByName("planilla para carga de datos");
    Logger.log("Valores a insertar:  " + values)
    var valuesNewSheet = ss.getRange(range).getValues();
    Logger.log("Rango: " + range + "valores del sheet antes: " + valuesNewSheet);
    ss.getRange(range).setValues(values);
    Logger.log("valores del sheet dsp: " + ss.getRange(range).getValues());
    Logger.log("new id: " + ss.getId());

  }catch(e){
    Logger.log("error: " + e )
    return;
  }

  return ss.getId();
}
1
Going trough a few tutorials might be in order.ScampMichael

1 Answers

2
votes

Changing the code to the following works with a plain new spreadsheet containing Sheet1, but sometimes I also get that server-error during the makeCopy-call. Added a retry for that and changed the lines containing ss.getId() to newSheet.getId() and added the stack-trace to the exception-logging.

function importData(id){
  if (id==undefined) {
    id="0AowkhKY0KeQAdDZCMXZPb2VOc1NoN2pFZk9Qd0xHNGc";
  }
  try{
    var sheet = SpreadsheetApp.openById(id);
    Logger.log("name of sheet: " + sheet.getName())
    var sheetToImport = sheet.getSheetByName("Sheet1");
    var sheetGeneric = DocsList.getFileById('0AowkhKY0KeQAdEZ0NFBkXzdMY05VVWFRa0J2bVJvelE');
    var newSheet;
    retried (5,"Permanent error while trying to copy sheet", function () {
      newSheet = sheetGeneric.makeCopy(sheet.getName()+" V2");
    });
    var row = sheetToImport.getRange('A:Z');
    var range = row.getA1Notation();
    var values = row.getValues();
    var ss = SpreadsheetApp.openById(newSheet.getId()).getSheetByName("Sheet1");
    Logger.log("Valores a insertar:  " + values)
    var valuesNewSheet = ss.getRange(range).getValues();
    Logger.log("Rango: " + range + "valores del sheet antes: " + valuesNewSheet);
    ss.getRange(range).setValues(values);
    Logger.log("valores del sheet dsp: " + ss.getRange(range).getValues());
    Logger.log("new id: " + newSheet.getId());
  }catch(e){
    Logger.log("error: " + e +"\n"+e.stack)
    return;
  }
  return newSheet.getId();
}

This server-errors sometimes happen and often it works when retrying after a short while. To easily repeat such a call, the following function is handy:

function retried (maxRetries, msg, f) {
  var retries=0;
  do {
    try {
      f ();
      if (retries>0) {
        Logger.log ("succeeded after "+retries+" retries for "+f);
      }
      return; // success;
    }
    catch(err) { 
      Logger.log ("catched "+err.toString()+"\n"+err.stack);
      retries++;
      Utilities.sleep (5000);
    }
  } while (retries<maxRetries);
  Logger.log (msg);
  throw msg;
}

which is used in the above code to retry the makeCopy-call