问题描述
我有一个遍历电子表格的脚本,该脚本从单元格中检索值并将这些值插入到文档中.该代码可以正常运行,但是我注意到当我复制文档的副本时,运行相同的代码并在代码内传递文档ID时,会出现以下错误:
I have a script that goes through a spreadsheet, retrieves values from the cells and inserts the values in a doc. The code works perfectly but I've noticed that when I make a copy of the doc, run the same code and pass the doc ID within the code I get the following error:
找不到具有给定ID的项目,或者您没有访问权限.
我尝试将文档公开,但出现相同的错误.有趣的是,如果我创建一个新文档并将该文档ID传递给该函数,它就可以正常工作.
I've tried to make the doc public but I get the same error. Interestingly if I create a new doc and pass that doc ID to the function it works just fine.
代码:
var templateid = "1IrEbukq3cVKg9MAPT-Aanfe4XCzoc-RCJKq6sOpQKGU"; // get template file id
var copyDoc = DocsList.getFileById(templateid).makeCopy(docName);
推荐答案
我不确定当您遇到问题时我是否完全理解,但我猜想是您尝试访问copyDoc时才知道.这是因为您尝试通过它的ID(如错误消息中显示的那样)来访问它,但是您没有它:从此处编写的代码中,copyDoc是一个文档,如果您希望其ID只需在.getId()
处添加这样的结局:
I'm not sure I understood exactly when you problem occurs but I'd guess it is when you try to access copyDoc.This is because you try to access it by its ID (as it appears from the error message) and you don't have it : from the code you write here copyDoc is a document, if you want its Id just add .getId()
at the end like this :
var copyDoc = DocsList.getFileById(templateid).makeCopy(docName).getId();
希望我猜对了;-)
您也可以这样操作,以便您可以使用所有东西:
You could also do that like this so you'll have everything ready to use :
var doctemplate = DocumentApp.openById("1IrEbukq3cVKg9MAPT-Aanfe4XCzoc-RCJKq6sOpQKGU");// this can be placed outside the function as a global variable
var docname="new name for the copy";
var copydoc=DocsList.copy(doctemplate,docname);
var copydocId = copydoc.getId();
jwesonga注释(thx)之后的代码中已更正错误
Edit : corrected error in code following jwesonga comment (thx)
这篇关于即使具有正确的ID,也无法复制文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!