问题描述
我使用的是importFromCSV脚本,第一次运行良好,但在脚本编辑器中出现警告,DocsList.getFiles()已弃用,应该找到替代方法。我试图直接用DriveApp.getFiles()换出,但是现在脚本超时了。
DriveApp.getFiles()不能与DocsList.getFiles ()?如果不是,我应该使用什么?或者是有什么我失踪导致超时?
函数importFromCSV(){
var fileName = 0B2n-RwpLExXnaXRBWG1aT3NLbm8\" ;
var files = DocsList.getFiles();
var csvFile =; (文件名[i] .getId()==文件名){
csvFile = {
for(var i = 0; i< files.length; i ++)文件[I] .getContentAsString();
休息;
}
}
var csvData = CSVToArray(csvFile,,);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
for(var i = 0; i< csvData.length; i ++){
sheet.getRange(i + 1,1,1,csvData [i] .length).setValues(new Array( csvData [I]));
}
}
答案是否定的
DocsList返回一组文件,而DriveApp返回一个。请参阅。
但是当我看到您的代码时,您没有通过名称获取文件,但是您使用 ID ...所以,您可以使用 DriveApp.getFileById('0B2n-RwpLExXnaXRBWG1aT3NLbm8')
来简化您的代码,它将立即返回正确的文件而不会迭代或循环...
I am using the importFromCSV script and it ran fine the first time but I got a warning in the script editor that the DocsList.getFiles() is deprecated and should find an alternative. I tried to directly swap out with DriveApp.getFiles() but then the script times out on that line now.
Is DriveApp.getFiles() not interchangeable with DocsList.getFiles()? If not, what should I be using? Or is there something else I am missing that causes the timeout?
function importFromCSV() {
var fileName = "0B2n-RwpLExXnaXRBWG1aT3NLbm8";
var files = DocsList.getFiles();
var csvFile = "";
for (var i = 0; i < files.length; i++) {
if (files[i].getId() == fileName) {
csvFile = files[i].getContentAsString();
break;
}
}
var csvData = CSVToArray(csvFile, ",");
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
for (var i = 0; i < csvData.length; i++) {
sheet.getRange(i+1, 1, 1, csvData[i].length).setValues(new Array(csvData[i]));
}
}
Short answer is no
DocsList returns an array of files while DriveApp returns a file iterator. See documentation here.
But when I see your code, you don't get the file by name but you use the ID... so you could simplify your code using DriveApp.getFileById('0B2n-RwpLExXnaXRBWG1aT3NLbm8')
that will immediately return the right file without iterating or looping...
这篇关于DriveApp.getFiles()与DocsList.getFiles()等价吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!