我需要我的脚本来加载要添加到电子表格的驱动器文件的 URL。无论出于何种原因,'File.getUrl()' 返回 null,但其他一切似乎都在工作。
例如,'File.getName()' 有效。所以我错过了许可还是什么?
这是我的代码(删节):
function loadURL(folderPath, name) {
var folder = loadFolder(folderPath);
// Find file
var files = folder.searchFiles('modifiedDate >= "' + getDate() + '" and title = "' + name + '"'),
file, found = 0;
for (; files.hasNext(); found++) {
file = files.next();
}
if (found == 1) {
/////////////////////////////////
// Here is 'getUrl'
/////////////////////////////////
var url = file.getUrl();
Logger.log("Loaded: " + url);
return url;
} else {
throw "Error loading file";
}
}
/**
* ** NOTE: THIS SEEMS TO BE WORKING FINE **
* Load a folder
* @param {string} url
* @return {Folder}
*/
function loadFolder(url) {
var folders = url.split('/'),
folder = DriveApp.getRootFolder();
Logger.log("Loading URL: " + url);
for (var key in folders) {
var fName = folders[key];
if (fName.length > 0) {
var fIt = folder.getFoldersByName(fName),
found = 0;
Logger.log(" -> " + fName);
for (; fIt.hasNext(); found++) {
folder = fIt.next();
}
if (found == 0) {
throw "Could not find the folder '" + fName + "'";
} else if (found > 1) {
throw "Found multiple matches to the folder '" + fName + "'";
}
}
}
return folder;
}
如果您好奇,请记录日志:
[14-02-26 19:56:15:980 EST] Loading URL: Work/PDF/
[14-02-26 19:56:15:981 EST] -> Work
[14-02-26 19:56:16:120 EST] -> PDF
[14-02-26 19:56:16:450 EST] Loaded: null
如果我将 getUrl 更改为 getName 它(有点)工作
[14-02-26 20:10:20:588 EST] Loading URL: Work/PDF/
[14-02-26 20:10:20:589 EST] -> Work
[14-02-26 20:10:20:727 EST] -> PDF
[14-02-26 20:10:21:058 EST] Loaded: Test