很抱歉,发生了服务器错误。请稍等一下,再试一次Google Apps脚本

时间:2013-01-29 14:34:27

标签: google-apps-script

请有人可以帮我吗?这是我的代码和抛出我们很抱歉,发生了服务器错误。请稍等一下再试一次。 这个想法是采用表格通用和另一张表格并将所有数据从表格复制到通用表格。

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 个答案:

答案 0 :(得分:2)

将代码更改为以下内容适用于包含Sheet1的普通新电子表格,但有时我也会在makeCopy调用期间收到该服务器错误。添加了重试并将包含ss.getId()的行更改为newSheet.getId(),并将堆栈跟踪添加到异常日志记录中。

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();
}

此服务器错误有时会发生,并且经常在短时间后重试时有效。为了轻松地重复这样的调用,以下功能非常方便:

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;
}

在上面的代码中用来重试makeCopy-call