繁体   English   中英

当我尝试使用 openByUrl() 或 openById() 访问电子表格时,Google Apps 脚本超时?

[英]Google Apps Script runs out of time when I try to access spreadsheet using openByUrl() or openById()?

提示:本站为国内最大中英文翻译问答网站,提供中英文对照查看,鼠标放在中文字句上可显示英文原文

我正在尝试从一个电子表格中获取 2 列(B、C)的数据并将其粘贴到另一个电子表格中。 它在昨天之前运行良好。 但是在过去的 2 天里,脚本用完了时间并且没有复制值就崩溃了。 有时它运行成功但只获取了总共12000行数据中的前1000行。 这是脚本:

function importRangeV1(sourceURL, sourceRange, destinationURL, sheetName){

  var sourceSS = SpreadsheetApp.openByUrl(sourceURL);
  var sourceRng = sourceSS.getRange(sourceRange)
  var sourceVals = sourceRng.getValues();

 
   SpreadsheetApp.flush();
  var destinationSS = SpreadsheetApp.openByUrl(destinationURL);
  var destSheet = destinationSS.getSheetByName(sheetName);
  destSheet.clearContents();

  var destRange = destSheet.getRange(1,1,sourceVals.length,sourceVals[0].length);
  destRange.setValues(sourceVals);  
}

现在上面代码中的以下 2 条语句在工作方面是相似的:

   var sourceSS = SpreadsheetApp.openByUrl(sourceURL);
   var destinationSS = SpreadsheetApp.openByUrl(destinationURL);

但是,此语句中出现了问题:

var destinationSS = SpreadsheetApp.openByUrl(destinationURL);

该语句花费了太多时间,以至于脚本抛出以下错误:

“服务电子表格在访问 ID 为 [此处为电子表格 ID] 的电子表格时超时”

这是指向destinationURL的链接: https://docs.google.com/spreadsheets/d/1T-vbfHdCNqULCY4voyxdPhR0v_EIxTsQZszfn9mWJ9Y/edit?usp=sharing

以下是我在工作表中运行此脚本的方式:

在此处输入图像描述

我发现这个问题与我的问题有点相似,但它没有解决 openByUrl() 或 openById(),在这方面的任何帮助将不胜感激。

看来您的工作表中充满了公式或数据,这是导致此问题的主要原因,我的一张工作表也遇到了同样的问题,那么这张工作表如何solution:

    function importRangeV1(sourceURL, sourceRange, destinationURL, sheetName){
       
     var sourceId = sourceURL.match(/\/d\/(.+)\//)[1];      //extract id from url
     var destId =   destinationURL.match(/\/d\/(.+)\//)[1]; //extract id from url
    
     var any = {};
     var srcValues = Sheets.Spreadsheets.Values.get(sourceId, sourceRange).values;
     Sheets.Spreadsheets.Values.clear(any,destId,sheetName+"!A1:B");
     Sheets.Spreadsheets.Values.update({values: srcValues}, destId, sheetName, {valueInputOption: "USER_ENTERED"});
};

此脚本使用Sheets API而不是openByUrl()来访问所需的工作表并将数据放在指定范围内。 测试一下,让我知道。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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