[英]How to refer to element in array in Google apps scripts
This is very basic, but I'm struggling to find the documentation for this.这是非常基本的,但我很难找到这方面的文档。
For example, I need to get a file id based on the file name.例如,我需要根据文件名获取文件 id。 Note that this is a simplified example and I may have more than one element in the array.
请注意,这是一个简化的示例,我可能在数组中有多个元素。
function getFileID() {
var fileNames = ["my_file"];
var Ids = [];
Ids.push(fileNames.getId());
Logger.log(Ids)
}
This will return TypeError: Cannot find function getID in object ["my_file"]
.这将返回
TypeError: Cannot find function getID in object ["my_file"]
。
So, it's reading fileNames
as an entire object and not just the string value that it contains.因此,它将
fileNames
作为整个 object 读取,而不仅仅是它包含的字符串值。
I tried the following, but these aren't right either.我尝试了以下方法,但这些也不正确。
function getFileID() {
var fileNames = ["my_file"];
var Ids = [];
Ids.push(fileNames[1].getId());
Logger.log(Ids)
}
function getFileID() {
var fileNames = ["my_file"];
var Ids = [];
var fileNames = (JSON.stringify(fileNames));
Ids.push(fileNames.getId());
Logger.log(Ids)
}
The reason it is not in the documentation is it is a JavaScript error.它不在文档中的原因是它是 JavaScript 错误。 Strings and arrays do not have a getId() function.
字符串和 arrays 没有 getId() function。 Google documents have a getId() function, but the string is not a google document, just a piece of text that contains the name of the google document.
Google 文档有一个 getId() function,但该字符串不是 google 文档,只是一段包含 google 文档名称的文本。
You need to use drive app to the a document based on it's name, then you can get the ID:您需要根据文件的名称对文件使用驱动应用程序,然后您可以获得ID:
function getFileID() {
var fileNames = ["my_file"];
var filename = fileNames[0]; //arrays start at 0
//this will get the id of the first file with that name
var file = DriveApp.getFilesByName(filename).next()
var id = file.getId();
var Ids = [];
Ids.push(id);
Logger.log(Ids)
}
Try changing this:尝试改变这个:
function getFileID() {
var fileNames = ["my_file"];
var Ids = [];
Ids.push(fileNames.getId());
Logger.log(Ids)
}
To something like this:对于这样的事情:
function getFileID() {
var folder=DriveApp.getFolderById("Folder Id");//might want to limit search to a folder
var fnA=["Linked 1","Linked 2","Linked 3","Linked 4","Linked 5"];//filenames I picked some names that I had in a folder
var ids=[];//array to collect names and ids
for(var i=0;i<fnA.length;i++) {
var files=folder.getFilesByName(fnA[i]);//often more than one file may have the same name
while(files.hasNext()) {
var file=files.next();
ids.push({name:fnA[i],id:file.getId()});//storing name and file id in each array element.
}
}
var html="";//building a dialog to view results
for(var i=0;i<ids.length;i++) {
html+=Utilities.formatString('<br />Filename: %s id: %s',ids[i].name,ids[i].id);
}
var userInterface=HtmlService.createHtmlOutput(html).setWidth(800);
SpreadsheetApp.getUi().showModelessDialog(userInterface, "File names and ids");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.