0

我需要我的脚本来加载要添加到电子表格的驱动器文件的 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
4

3 回答 3

1

使用 id 而不是 url 有效:

var url = "https://docs.google.com/document/d/" + file.getId() + "/";
于 2014-02-27T01:49:21.083 回答
0
   var url = DocsList.getFileById(fileId).getUrl();

Was recommended as a workaround by a poster on the issue thread that zig posted.

So, in your case, you'd use:

  for (; files.hasNext(); found++) {
    file = files.next();
  }

  if (found == 1) {
    /////////////////////////////////
    // Here is 'getUrl'
    /////////////////////////////////
    var fileId = file.getId();//new line added to getId of the file

    //modified url line to reference doc by id
    var url = DocsList.getFileById(fileId).getUrl();

    Logger.log("Loaded: " + url);
    return url;
  } else {
    throw "Error loading file";
  }

I didn't get to test this right now because it's late and I'd have to build a the right structure to test it properly, but since you already have the structure change those two commented lines and let me know if that worked.

于 2014-02-27T04:05:55.070 回答
0

今天开始出现驱动器 getUrl 的问题,谷歌已经意识到它并正在处理它。使用 docList 或驱动高级服务工作。

于 2014-02-27T03:25:24.487 回答