0

我希望获得 GDrive 中所有文件夹的几个属性并将这些属性写入电子表格。由于文件夹数量众多(超过 300 个),我决定使用分页和批处理。这似乎有效,但我似乎无法将我在批处理中创建的 Array[][] 写入电子表格。

当我尝试在电子表格中设置值时出现以下错误:

无法将 (class)@3cc8188e 转换为 Object[][]。

我没有找到与我的问题相似的任何列出的问题。

出现错误时,脚本的最后一行会突出显示。代码是:

function myFunction() {
  var folder = DocsList.getFolder('MyFolder');
  var subfolders = null;
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var cell = sheet.getRange('a1');
  var x = 0;
  var pageSize = 250;
  var token = null;
  var xfolders = new Array(500);

  do {
       var resultset = folder.getFoldersForPaging(pageSize, token);
       subfolders = resultset.getFolders();
       token = resultset.getToken();
       x = subfolders.length;

       for (var a = 0; a < subfolders.length; a++) {
          var contents = subfolders[a].getFiles();
          xfolders[a] = new Array(6);
          if(contents.length>0) {
             xfolders[a][0] = subfolders[a].getName();
             xfolders[a][1] = subfolders[a].getDateCreated();
             xfolders[a][2] = subfolders[a].getLastUpdated();
             xfolders[a][3] = contents.length;
             xfolders[a][4] = subfolders[a].getSize();
             xfolders[a][5] = a;
          }
       }
  } while (subfolders.length > pageSize)

sheet.getRange(1, 1, x, 6).setValues(xfolders); }

4

1 回答 1

0

xfolders您已经从一个已初始化长度的数组开始。没有必要这样做——知道它是一个数组就足够了。稍后您getRange()使用rows==x, where x = subfolders.length... 调用,如果不是 500( 的长度xfolders),您最终会遇到错误。(...因为您有未定义的数组元素。)

您需要做的是确保您写入的范围与您呈现的值具有相同的尺寸。一种方法是使用xfolders自身计算范围尺寸。

这是您的函数,经过重构以动态增长xfolders,然后使用其维度输出到电子表格。

function myFunction() {
  var folder = DocsList.getFolder('MyFolder');
  var subfolders = null;
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var cell = sheet.getRange('a1');
  var pageSize = 250;
  var token = null;
  var xfolders = [];

  do {
       var resultset = folder.getFoldersForPaging(pageSize, token);
       subfolders = resultset.getFolders();
       token = resultset.getToken();

       for (var a in subfolders) {
          var contents = subfolders[a].getFiles();
          xfolders[a] = [];
          if(contents.length>0) {
             xfolders[a][0] = subfolders[a].getName();
             xfolders[a][1] = subfolders[a].getDateCreated();
             xfolders[a][2] = subfolders[a].getLastUpdated();
             xfolders[a][3] = contents.length;
             xfolders[a][4] = subfolders[a].getSize();
             xfolders[a][5] = a;
          }
       }
  } while (subfolders.length == pageSize)  // Quit when we aren't getting enough folders
  sheet.getRange(1, 1, xfolders.length, xfolders[0].length).setValues(xfolders); 
}
于 2013-04-09T18:03:08.603 回答