简体   繁体   English

通过 Google Apps Scripts #2 发布 Google 电子表格

[英]Publish a Google Spreadsheet through Google Apps Scripts #2

I am trying to publish a web app through the script code.我正在尝试通过脚本代码发布一个网络应用程序。 I read this post but I can't make it work, maybe it's a slightly different situation.我读了这篇文章,但我无法让它工作,也许情况略有不同。 I have got a script that copies a spreadsheet: in the script, I want to publish the new copy of the spreadsheet as a web app and, if it's possibile, retrieve its public url (anyone should be able to access to it).我有一个复制电子表格的脚本:在脚本中,我想将电子表格的新副本发布为网络应用程序,如果可能,检索其公共 URL(任何人都应该能够访问它)。

Here it is my code:这是我的代码:

  // Make a copy of the spreadsheet
  var repliesFile = responseTemplate.makeCopy('risposte', newFolder);
  var repliesId = SpreadsheetApp.openById(repliesFile.getId());

  // pubblish the new copy as a web app
  Drive.Revisions.update({published: true, publishedOutsideDomain: true, publishAuto: true}, repliesId, 1);

  // of course, this doesn't work.. I need a way to get the web app public url of the new copy
  var webAppUrl = ScriptApp.getService().getUrl(); 

I got an 404 error.我收到 404 错误。 What am I doing wrong?我究竟做错了什么? Drive Api v.2 is enabled in the script. Drive Api v.2 在脚本中启用。 Thanks for any help you can give me!感谢你给与我的帮助!

  • You want to publish the copied Spreadsheet.您要发布复制的电子表格。
  • You want to retrieve the URL of the published Spreadsheet.您想要检索已发布电子表格的 URL。
  • You want to achieve this by modifying your script.您想通过修改脚本来实现这一点。

If my understanding is correct, how about this answer?如果我的理解是正确的,这个答案怎么样? Please think of this as just one of several possible answers.请将此视为几种可能的答案之一。

Modification points:改装要点:

  • In your script, I think that repliesId is not correct.在您的脚本中,我认为repliesId不正确。 It's the Spreadsheet object.它是电子表格对象。 Please use repliesFile.getId() as the file ID.请使用repliesFile.getId()作为文件 ID。
  • Unfortunately, in the current stage, the URL like https://docs.google.com/spreadsheets/d/e/2PACX-###/pubhtml cannot be retrieved.不幸的是,在当前阶段,无法检索到像https://docs.google.com/spreadsheets/d/e/2PACX-###/pubhtml这样的 URL。 But you can https://docs.google.com/spreadsheet/pub?key=### spreadsheetId ### as the URL of published Spreadsheet.但是您可以将https://docs.google.com/spreadsheet/pub?key=### spreadsheetId ###作为已发布电子表格的 URL。

Modified script:修改后的脚本:

When your script is modified, please modify as follows.当您的脚本被修改时,请进行如下修改。

From: 从:
 Drive.Revisions.update({published: true, publishedOutsideDomain: true, publishAuto: true}, repliesId, 1);
To: 到:
 var fileId = repliesFile.getId(); Drive.Revisions.update({published: true, publishedOutsideDomain: true, publishAuto: true}, fileId, 1); var url = "https://docs.google.com/spreadsheet/pub?key=" + fileId;
  • url can be used as the URL of the published Spreadsheet. url可用作已发布电子表格的 URL。

Note:笔记:

  • In this case, please enable Drive API at Advanced Google services.在这种情况下,请在高级 Google 服务中启用 Drive API。

References:参考:

If I misunderstood your question and this was not the result you want, I apologize.如果我误解了您的问题并且这不是您想要的结果,我深表歉意。

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

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