如何用Apps Script通过Google Doc的URL或DocumentApp ID访问Drive文件?
问题描述
用户编写了以下Google Apps Script代码,意图通过Google Doc的URL获取文件并复制到新文件夹:
function copyToGoogleDoc() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var thisFileId = ss.getId(); var thisFile = DriveApp.getFileById(thisFileId); var parentFolder = thisFile.getParents().next(); var newFolderName = ss.getRangeByName("newFolderName").getValue(); var newFolder = parentFolder.createFolder(newFolderName); var resumeName = ss.getRangeByName("resumeName").getValue(); var resumeLink = ss.getRangeByName("resumeLink").getValue(); var resumeDoc = DocumentApp.openByUrl(resumeLink); var resumeDocId = resumeDoc.getId(); var resumeFile = DriveApp.getFileById(resumeDocId); resumeFile.makeCopy(resumeName, newFolder); }
运行时出现错误:
Exception: The document is inaccessible. Please try again later.
错误指向代码行var resumeFile = DriveApp.getFileById(resumeDocId);。用户疑惑DocumentApp ID与DriveApp ID是否存在差异,且确认自己拥有文件权限,寻求通过Google Doc的URL或DocumentApp ID访问Drive文件的正确方法。
解答
关于ID的一致性
DocumentApp和DriveApp使用的文件ID完全一致。Google Docs文件本质就是Google Drive中的存储文件,ID是同一个唯一标识,不存在两种服务ID不同的情况。
错误原因与解决方案
原代码报错的核心原因是:虽然DocumentApp.openByUrl()能正常访问文档,但跨服务调用时(从DocumentApp到DriveApp),权限上下文可能出现异常。直接简化流程、减少跨服务调用即可解决问题。
方案1:直接从URL提取ID并复制(推荐)
跳过DocumentApp的调用,直接从Google Doc链接中提取ID,用DriveApp完成复制操作,流程更简洁且避免权限问题:
function copyToGoogleDoc() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var thisFile = DriveApp.getFileById(ss.getId()); var parentFolder = thisFile.getParents().next(); var newFolderName = ss.getRangeByName("newFolderName").getValue(); var newFolder = parentFolder.createFolder(newFolderName); var resumeName = ss.getRangeByName("resumeName").getValue(); var resumeLink = ss.getRangeByName("resumeLink").getValue(); // 从Google Doc链接中提取文件ID(适配常见链接格式) var idMatch = resumeLink.match(/d\/([a-zA-Z0-9-_]+)/); if (!idMatch) { throw new Error("无效的Google Doc链接"); } var resumeDocId = idMatch[1]; // 直接获取文件并复制到目标文件夹 var resumeFile = DriveApp.getFileById(resumeDocId); resumeFile.makeCopy(resumeName, newFolder); }
方案2:保留DocumentApp调用的修正
如果必须通过DocumentApp打开文档(比如需要先编辑内容),可以在调用DriveApp前确保文档保存,并直接使用DocumentApp返回的ID:
function copyToGoogleDoc() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var thisFile = DriveApp.getFileById(ss.getId()); var parentFolder = thisFile.getParents().next(); var newFolderName = ss.getRangeByName("newFolderName").getValue(); var newFolder = parentFolder.createFolder(newFolderName); var resumeName = ss.getRangeByName("resumeName").getValue(); var resumeLink = ss.getRangeByName("resumeLink").getValue(); var resumeDoc = DocumentApp.openByUrl(resumeLink); // 确保文档已保存(若有编辑操作则必须,无编辑可省略) resumeDoc.saveAndClose(); var resumeDocId = resumeDoc.getId(); var resumeFile = DriveApp.getFileById(resumeDocId); resumeFile.makeCopy(resumeName, newFolder); }
关键说明
- 确保Google Doc链接格式正确:常见格式为
https://docs.google.com/document/d/[文件ID]/edit...,正则表达式d\/([a-zA-Z0-9-_]+)可稳定提取ID。 - 若仍出现权限问题,可检查脚本的授权范围:确保脚本已获得
https://www.googleapis.com/auth/drive的授权权限(执行脚本时会自动请求)。
内容的提问来源于stack exchange,提问作者William Toscano
相关产品推荐
相关产品推荐

