我正在尝试使用Apps Script获取我的Google工作表的网页链接。当您转到“文件”>“发布到网络”>“发布”时,您将在其中看到一个网络链接。我想通过应用脚本获得该链接。请帮忙。
https://docs.google.com/spreadsheets/d/e/2PACX-###/pubhtml
使用Google Apps脚本一样检索URL 。如果我的理解是正确的,那么这个答案呢?
File>Publish to the web>publish
可以针对Google文档运行。publishedLink
之前可以使用Drive API v2检索此URL 。但是很遗憾,在当前阶段,这不能同时用于Drive API v2和v3。因此,作为一种解决方法,我建议使用文件ID。
顺便说一下,2PACX-###
ofhttps://docs.google.com/spreadsheets/d/e/2PACX-###/pubhtml
不是文件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 Document中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)
}
如果我误解了您的问题,而这不是您想要的方向,我深表歉意。
如果您可以在Advanced Google Services启用Drive API,则还可以使用以下脚本。
当您使用Google文档的容器绑定脚本时,可以使用它。
var url = Drive.Files.get((DocumentApp.getActiveDocument() || SpreadsheetApp.getActiveSpreadsheet() || SlidesApp.getActivePresentation()).getId()).alternateLink.replace(/\/edit.+/, "/pub");
Logger.log(url);
当您拥有Google文档的文件ID时,就可以使用它。
var url = Drive.Files.get(fileId).alternateLink.replace(/\/edit.+/, "/pub");
Logger.log(url);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句