【发布时间】:2019-10-31 12:39:51
【问题描述】:
我正在尝试使用 Apps 脚本获取我的 google 表格的网页链接。当您转到文件>发布到网络>发布时,您将在那里看到一个网络链接。我想通过应用程序脚本获取该链接。请帮忙。
【问题讨论】:
-
@Tanaike 你是对的。谢谢你。
我正在尝试使用 Apps 脚本获取我的 google 表格的网页链接。当您转到文件>发布到网络>发布时,您将在那里看到一个网络链接。我想通过应用程序脚本获取该链接。请帮忙。
【问题讨论】:
https://docs.google.com/spreadsheets/d/e/2PACX-###/pubhtml 这样的 URL。如果我的理解是正确的,那么这个答案呢?
File>Publish to the web>publish 可以为 Google Docs 运行。之前可以使用 Drive API v2 的publishedLink 检索此 URL。但不幸的是,在当前阶段,这不能同时用于 Drive API v2 和 v3。因此,作为一种解决方法,我想建议使用文件 ID。
顺便说一下,https://docs.google.com/spreadsheets/d/e/2PACX-###/pubhtml 的 2PACX-### 不是文件 ID。也请注意这一点。
在这种情况下,作为测试用例,检索已发布的电子表格的 URL。
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var url = ss.getUrl().replace("edit", "pubhtml"); // or replace("edit", "pub");
Logger.log(url)
}
例如,如果要检索 Google Document 的 URL,可以使用以下脚本。不幸的是,在 Google 文档中,getUrl() 返回https://docs.google.com/open?id=###。所以上面的脚本不能用。
function myFunction() {
var doc = DocumentApp.getActiveDocument();
var url = "https://docs.google.com/document/d/" + doc.getId() + "/pub";
Logger.log(url)
}
如果我误解了您的问题并且这不是您想要的方向,我深表歉意。
如果you can enable Drive API at Advanced Google Services,也可以使用下面的脚本。
当你使用谷歌文档的容器绑定脚本时,可以使用这个。
var url = Drive.Files.get((DocumentApp.getActiveDocument() || SpreadsheetApp.getActiveSpreadsheet() || SlidesApp.getActivePresentation()).getId()).alternateLink.replace(/\/edit.+/, "/pub");
Logger.log(url);
当你有 Google Docs 的文件 ID 时,你可以使用它。
var url = Drive.Files.get(fileId).alternateLink.replace(/\/edit.+/, "/pub");
Logger.log(url);
【讨论】: