【发布时间】:2018-10-26 19:19:50
【问题描述】:
我的 Google 应用脚本有问题 我正在使用 php 将文件上传到驱动器,该文件正在成功插入驱动器。但是当我想编辑我的文档以使用谷歌应用脚本突出显示单词时,我收到错误,谷歌驱动器和谷歌文档的 文件 ID 不同,所以我无法在谷歌中编辑文件使用应用脚本的文档。请查看我的代码并建议我
我正在使用谷歌文档链接进行编辑,但我拥有的文件 ID 是驱动器文件 ID,所以建议我如何获得它
function doGet(param){
var files = DriveApp.getFilesByName('11008.doc');
while (files.hasNext()) {
var file = files.next();
var url = file.getUrl();
url = url.replace("?usp=drivesdk","");
Logger.log(url);
var doc = DocumentApp.openByUrl('https://docs.google.com/document/d/18CEwLeTLwsQ_xR2BLWqs5E1lQrd9SsNuUwlnBpWLQro/edit');
var textToHighlight = 'MA';
var highlightStyle = {};
highlightStyle[DocumentApp.Attribute.FOREGROUND_COLOR] = '#FF0000';
var paras = doc.getParagraphs();
var textLocation = {};
var i;
for (i=0; i<paras.length; ++i) {
textLocation = paras[i].findText(textToHighlight);
if (textLocation != null && textLocation.getStartOffset() != -1) {
textLocation.getElement().setAttributes(textLocation.getStartOffset(),textLocation.getEndOffsetInclusive(), highlightStyle);
}
}
return doc;
}
}
【问题讨论】:
-
这是从 url 获取文件 ID 的最简单方法。使用正则表达式,请参阅此SO post 中的示例。您也可以查看this discussion from google help forum 了解更多信息。
标签: php google-apps-script google-drive-api