我希望获得gdrive中所有文件夹的几个属性,并将这些属性写入电子表格。由于文件夹数量很多(超过300个),我决定使用分页和批处理。这似乎有效,但我似乎无法将批量处理中创建的Array [] []写入电子表格。
当我尝试在电子表格中设置值时,我收到以下错误: 无法转换(类)@ 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); }
答案 0 :(得分:0)
您已经开始使用已初始化长度的数组xfolders
。没有必要这样做 - 足以知道它是一个数组。之后,您使用getRange()
拨打rows==x
,其中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);
}