【问题标题】:Avoid UrlFetchApp Timeout Error in Excel Conversion Google Scripts避免 Excel 转换 Google 脚本中的 UrlFetchApp 超时错误
【发布时间】:2020-01-06 03:20:12
【问题描述】:

以前的 StackOverflow 帖子报告说 UrlFetchApp 在超过一分钟的运行时间后终止。这发生在我身上并且正在影响我的程序。该程序自动将电子表格转换为 Excel 文件作为备份和保存数据。电子表格有很多数据,所以现在超时。我对该程序的替代解决方案是什么?我的 google 电子表格不是很大,也没有达到 google sheet 的最大阈值,因此不需要清除数据的难度。

function downloadSpreadSheet(){
  var spreadsheet_id =  "INSERT_ID_HERE";    //spreadsheet_id is the id of the google sheet, found in URL
  var ss = SpreadsheetApp.openById(spreadsheet_id);
  var now = new Date();

  var driveFolder = 'Dispatch Backup';                                     //driveFolder is the name of the destination folder on Google Drive
  var newFilename = 'Exported ' + (now.getMonth()+1) + '-' + now.getDate() + "-" + now.getYear();  //newFilename is the name of the .xlsx file to be written out
  var folder = DriveApp.getFoldersByName(driveFolder);                     //folder is a collection of all folders in the user's Drive with that name


  var params = {                                                           //params are the parameters used to grant authority to copy and save the new file
      method      : "get",
      headers     : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
      muteHttpExceptions: true
  };


  //url is html around the spreadsheet ID that converts the sheet into Excel format
  var url = "https://docs.google.com/feeds/download/spreadsheets/Export?key="+spreadsheet_id+"&exportFormat=xlsx";
  var doc = UrlFetchApp.fetch(url, params).getBlob();   //TIMES OUT HERE                                 //doc contains the converted sheet data

  //finds the location of driveFolder or creates one if not present
  if (folder.hasNext()) {
    folder = folder.next();
  } else {
    folder = DriveApp.createFolder(driveFolder);
  }

  //Creates a file in the root of the user's Drive from a given Blob of arbitrary data.
  file= folder.createFile(doc);
  //Sets the name of the File.
  if (newFilename != "")
          file.setName(newFilename)

}

感谢任何帮助或建议。谢谢。

【问题讨论】:

  • 尝试使用提要网址的其他方法。有关详细信息,请查看 Mogsdad 回复 stackoverflow.com/q/27277058/1595451
  • 感谢您提供。我已经尝试过他的代码,但仍然失败。他在他的脚本中使用了 UrlFetchApp,所以我再次遇到超时错误也就不足为奇了。

标签: google-apps-script


【解决方案1】:

对您的问题进行一些研究,我遇到的其他人也遇到了同样的问题,并且已在 issue tracker 上报告,您可以在那里发表评论以保持线程活跃。

【讨论】:

  • 嗨,阿尔贝托,谢谢你。它似乎可以工作,直到我下载它并在打开它时出现错误:“Excel 无法打开 fie 'FILE_NAME',因为文件格式或文件扩展名无效。验证文件没有损坏并且文件扩展名匹配文件的格式”。你知道是什么问题吗?
  • 我正在编辑我的答案,因为要将文件导出到 .xlsx,它必须使用 Files: export 端点,但使用 Apps 脚本时,issue tracker 上报告了一个错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-29
  • 1970-01-01
  • 2020-08-31
  • 1970-01-01
  • 2013-04-21
  • 2011-11-26
相关资源
最近更新 更多