简体   繁体   English

从 google drive AppScript Google Sheet 下载 PDF 文件

[英]Download PDF file from google drive AppScript Google Sheet

I created a button in google sheets and I'm trying to download file from google drive with ID that I store in a cell.我在 google 表格中创建了一个按钮,我正在尝试从 google 驱动器下载文件,其中的 ID 存储在一个单元格中。 I searched stackoverflow and I found some code that might work, but it doesn't.我搜索了 stackoverflow,我发现了一些可能有效的代码,但它没有。 When I assign the script to the button and I click it, it says "We're sorry, a server error occurred. Please wait a bit and try again."当我将脚本分配给按钮并单击它时,它会显示“很抱歉,发生服务器错误。请稍等一下,然后重试。”

That's the code I'm trying to use:那是我要使用的代码:

function doGet(e) {
  return HtmlService.createHtmlOutput(downloader());
}

function downloader() {
  var mySheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("File Lookup");
  var File_LinkID = mySheet.getRange("J4").getValue();

  var out = "<body onload='dllink.click()'>";
  out +="<a id='dllink' href='https://drive.google.com/uc?export=download&id="+File_LinkID+"'>wait will download automatically<a/>";
  out +="</body>";
  return out;
};

Thank you!谢谢!

In your situation, how about the following modification?在您的情况下,以下修改如何?

From:从:

out +="<a id='dllink' href='https://drive.google.com/uc?export=download&id="+File_LinkID+"'>wait will download automatically<a/>";

To:到:

out +="<a id='dllink' target='_blank' href='https://drive.google.com/uc?export=download&id="+File_LinkID+"'>wait will download automatically<a/>";
  • In this modification, target='_blank' is added.本次修改,增加了target='_blank'

Note:笔记:

  • If an error is related to the permission, please publicly share the file and test it again.如果错误与权限有关,请公开分享文件并重新测试。
  • In your script, it seems that Web Apps is used.在您的脚本中,似乎使用了 Web Apps。 In this case, when you modified the Google Apps Script, please modify the deployment as a new version.在这种情况下,当您修改 Google Apps 脚本时,请将部署修改为新版本。 By this, the modified script is reflected in Web Apps.至此,修改后的脚本反映在Web Apps中。 Please be careful this.请注意这一点。
  • You can see the detail of this in the report of " Redeploying Web Apps without Changing URL of Web Apps for new IDE ".您可以在“ 为新的 IDE 重新部署 Web 应用程序而无需更改 URL 的 Web 应用程序”的报告中查看详细信息。

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

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