1

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

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

1 回答 1

3

将代码更改为以下代码适用于包含 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 调用

于 2013-01-29T21:09:59.393 回答