【问题标题】:Publish a Google Spreadsheet through Google Apps Scripts #2通过 Google Apps 脚本发布 Google 电子表格 #2
【发布时间】:2020-03-23 08:31:01
【问题描述】:

我正在尝试通过脚本代码发布 Web 应用程序。我读了this post,但我无法让它工作,也许情况略有不同。我有一个复制电子表格的脚本:在脚本中,我想将电子表格的新副本发布为网络应用程序,如果可能,检索其公共 url(任何人都应该能够访问它)。

这是我的代码:

  // 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(); 

我收到 404 错误。我究竟做错了什么?脚本中启用了 Drive Api v.2。感谢您能给我的任何帮助!

【问题讨论】:

    标签: google-apps-script google-sheets google-drive-api


    【解决方案1】:
    • 您想要发布复制的电子表格。
    • 您想要检索已发布电子表格的 URL。
    • 您想通过修改脚本来实现。

    如果我的理解是正确的,那么这个答案呢?请认为这只是几个可能的答案之一。

    修改点:

    • 在您的脚本中,我认为repliesId 不正确。它是电子表格对象。请使用repliesFile.getId() 作为文件ID。
    • 很遗憾,在当前阶段,无法检索到https://docs.google.com/spreadsheets/d/e/2PACX-###/pubhtml 之类的URL。但您可以将https://docs.google.com/spreadsheet/pub?key=### spreadsheetId ### 作为已发布电子表格的网址。

    修改脚本:

    当你的脚本被修改时,请进行如下修改。

    从:
    Drive.Revisions.update({published: true, publishedOutsideDomain: true, publishAuto: true}, repliesId, 1);
    
    到:
    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 可用作已发布电子表格的 URL。

    注意:

    • 在这种情况下,请在高级 Google 服务中启用 Drive API。

    参考资料:

    如果我误解了您的问题并且这不是您想要的结果,我深表歉意。

    【讨论】:

    • 哇,这对我来说是一个非常愚蠢的错误。非常感谢你。现在,问题是 url 指向电子表格的一种公共版本(我可以看到里面的所有数据和表格),而不是复制电子表格中 doGet() 函数返回的 html 输出(有一个有界脚本)。你能解释一下为什么吗?
    • @Luther B 感谢您的回复。我很高兴你的问题得到了解决。我愿意支持你。但是,不幸的是,我无法理解您的附加问题。这是因为我的英语水平不好。我必须为此道歉。那么您可以通过包含详细信息将其作为新问题发布吗?借此,我想确认您的情况并考虑一下。此外,它还将帮助其他用户思考解决方案。如果您能合作解决您的问题。我很高兴。
    • 我刚刚发布了另一篇文章。我希望它得到很好的解释。谢谢!
    • @Luther B 感谢您的回复。我刚才看到了你的新问题。我注意到已经发布了答案。我认为它会解决您的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    • 2013-09-12
    • 2022-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多