Google Apps Script运行报错:Exception: Document is missing 求助
问题:Google Apps Script 访问Google Doc报错"Document is missing"
问题描述
首次使用Google Apps Script编写代码,意图提取Google Doc中书签标记的内容到Google Sheet单元格,已将Google Doc和Google Sheet设置为「任何有链接的人均可访问」权限,但运行代码时仍报错:
Exception: Document is missing (perhaps it was deleted, or you don't have read access?)
用户提供的代码
function getTextFromDoc() { var docId = 'https://docs.google.com/document/d/1KuEhKWTCqLY4mlT1cUHLU3sQbVR2wuS6-QPo8XUFvzw/edit?usp=sharing'; // Replace with your Google Doc ID var bookmarkId = 'https://docs.google.com/document/d/1KuEhKWTCqLY4mlT1cUHLU3sQbVR2wuS6-QPo8XUFvzw/edit?pli=1#bookmark=id.mfymbqmkxh1i'; // Replace with your Bookmark ID var doc = DocumentApp.openById(docId); var bookmarks = doc.getBookmarks(); for (var i = 0; i < bookmarks.length; i++) { if (bookmarks[i].getId() === bookmarkId) { var startElement = bookmarks[i].getPosition().getElement(); var body = doc.getBody(); var text = ''; var started = false; // Iterate through all elements in the document for (var j = 0; j < body.getNumChildren(); j++) { var element = body.getChild(j); if (element === startElement) { started = true; } if (started) { if (element.getType() === DocumentApp.ElementType.PARAGRAPH) { text += element.asParagraph().getText() + '\n'; } else if (element.getType() === DocumentApp.ElementType.TEXT) { text += element.asText().getText() + '\n'; } // Stop at the next heading or the end of the document if (element.getType() === DocumentApp.ElementType.PARAGRAPH && element.asParagraph().getHeading() !== DocumentApp.ParagraphHeading.NORMAL) { break; } } } // Paste text into Google Sheet var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); sheet.getRange('A1').setValue(text.trim()); // Set the content in cell A1 break; } } }
解决方案
1. 修正文档ID的使用
DocumentApp.openById()方法仅接受Google Doc的纯ID,而非完整的分享链接。从你的文档链接中提取ID:
原链接:https://docs.google.com/document/d/1KuEhKWTCqLY4mlT1cUHLU3sQbVR2wuS6-QPo8XUFvzw/edit?usp=sharing
提取的ID:1KuEhKWTCqLY4mlT1cUHLU3sQbVR2wuS6-QPo8XUFvzw
2. 修正书签ID的使用
bookmarks[i].getId()返回的是书签的纯标识符(不带链接),你需要从书签链接中提取#bookmark=id.后的部分:
原书签链接:https://docs.google.com/document/d/1KuEhKWTCqLY4mlT1cUHLU3sQbVR2wuS6-QPo8XUFvzw/edit?pli=1#bookmark=id.mfymbqmkxh1i
提取的书签ID:mfymbqmkxh1i
3. 修正后的代码
function getTextFromDoc() { // 使用纯文档ID替换完整链接 var docId = '1KuEhKWTCqLY4mlT1cUHLU3sQbVR2wuS6-QPo8XUFvzw'; // 使用纯书签ID替换带链接的字符串 var bookmarkId = 'mfymbqmkxh1i'; var doc = DocumentApp.openById(docId); var bookmarks = doc.getBookmarks(); for (var i = 0; i < bookmarks.length; i++) { if (bookmarks[i].getId() === bookmarkId) { var startElement = bookmarks[i].getPosition().getElement(); var body = doc.getBody(); var text = ''; var started = false; // Iterate through all elements in the document for (var j = 0; j < body.getNumChildren(); j++) { var element = body.getChild(j); if (element === startElement) { started = true; } if (started) { if (element.getType() === DocumentApp.ElementType.PARAGRAPH) { text += element.asParagraph().getText() + '\n'; } else if (element.getType() === DocumentApp.ElementType.TEXT) { text += element.asText().getText() + '\n'; } // Stop at the next heading or the end of the document if (element.getType() === DocumentApp.ElementType.PARAGRAPH && element.asParagraph().getHeading() !== DocumentApp.ParagraphHeading.NORMAL) { break; } } } // Paste text into Google Sheet var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); sheet.getRange('A1').setValue(text.trim()); // Set the content in cell A1 break; } } }
额外注意事项
- 即使文档设为公开,脚本运行时的权限基于当前执行脚本的Google账号,请确认该账号能直接打开目标文档(复制文档链接在浏览器打开测试)。
- 如果仍有权限问题,可以尝试将文档共享给脚本执行账号的邮箱,确保拥有至少「查看」权限。
内容的提问来源于stack exchange,提问作者Ingrid Koh
相关产品推荐
相关产品推荐

