类型错误:在对象中找不到函数 hasNext(带有日期 ID 的文件夹) [英] TypeError: Cannot find function hasNext in object (Folder with date ID)
问题描述
我是 JavaScript 和 google 应用程序编码的新手.我正在制作一个数据库,每晚都会自动将新用户更新到汇总表中.
I'm new to coding in javascript and google apps. I'm making a database that will automatically update new users into an aggregate sheet nightly.
我的 searchfolder() 函数无法执行,因为我不断收到错误TypeError:无法在对象中找到函数 hasNext 星期四 7 月 14 日 09:48:01 PDT 2016 信息:目标.(第 24 行,文件每天工作)"
My searchfolder() function won't execute because i keep getting the error "TypeError: Cannot find function hasNext in object Thu Jul 14 09:48:01 PDT 2016 INFO: Target. (line 24, file working daily)"
var 文件在 DriveApp.getFolderById(folderId) 之后有 .getfiles 所以我一点也不知道问题是什么
var files has .getfiles after DriveApp.getFolderById(folderId) so i haven't the slightest idea what the problem is
代码:
var counter = 0;
function timeStamp(){
counter= (counter+1)
}
var files = "null"
function searchFolder() {
var folderId = '0B6wmHZ5c0fzfTjI1bFpKOHI3N3M'; // test folder
// Log the name of every file in the folder.
var files = DriveApp.getFolderById(folderId).getFiles(); //log files in
folder
while (files.hasNext()) { //loop log files in folder
var file = files.next(); //log files in folder
Logger.log(file.getName()); //logs name of file in folder
var files = Logger.getLog();
}
}
function autoUpdate(){ //updates monthly from newly imported daily
if (counter == 1){ //counter is made to be 1 when day is uploaded to
monthly
var ss =
SpreadsheetApp.openById("1lH9Y12P2Q2OFndIJoAU48ePggXFc9WGcWjolZMcABoc");
//defines target spreadsheet ie monthly
SpreadsheetApp.setActiveSpreadsheet(ss); //sets target spreadsheet as
active
var range= ss.getRange("A1:A1");
range.activate; // activates range
range.setValue('=IMPORTRANGE("1K7Rj4QK-
EVjf8lZ0BSew7iDeCtktqWjzgjoaVPe5jSc","sheet1!A1:G6")'); //Puts in
IMPORTRANGE into target as a STRING value (just words). Once it hits the
sheet, then SHEETS executes IMPORTRANGE not SCRIPTS. In Source sheet,
range is selected to import to target (ie A1:G6)
counter=(counter-1)
}
}
//searchFolder();
timeStamp();
autoUpdate();
推荐答案
您两次声明了变量 files
.此外,推入日志然后取回日志是不好的做法.最好使用数组代替.您收到错误,因为您存储文件名的变量 files
在第一次循环后更改为日志上下文,这意味着您的 files 中没有更多函数
hasNext
对象(它不是 string
类型).
You declared variable files
two times. Also, pushing into log and then getting logs back is bad practice. Better use array instead. You got an error because variable files
where you store file names is changed to log context after first loop, which means that there is no more function hasNext
inside your files
object (it is not of type string
).
var counter = 0;
function timeStamp(){
counter= (counter+1)
}
var files = [];
function searchFolder() {
var folderId = '0B6wmHZ5c0fzfTjI1bFpKOHI3N3M'; // test folder
// Log the name of every file in the folder.
var filesN = DriveApp.getFolderById(folderId).getFiles(); //log files in folder
while (filesN.hasNext()) files.push(filesN.next().getName());
}
function autoUpdate(){ //updates monthly from newly imported daily
if (counter == 1){ //counter is made to be 1 when day is uploaded to monthly
var ss = SpreadsheetApp.openById("1lH9Y12P2Q2OFndIJoAU48ePggXFc9WGcWjolZMcABoc");
//defines target spreadsheet ie monthly
SpreadsheetApp.setActiveSpreadsheet(ss); //sets target spreadsheet as
active
var range= ss.getRange("A1:A1");
range.activate; // activates range
range.setValue('=IMPORTRANGE("1K7Rj4QK-EVjf8lZ0BSew7iDeCtktqWjzgjoaVPe5jSc","sheet1!A1:G6")'); //Puts in IMPORTRANGE into target as a STRING value (just words). Once it hits the sheet, then SHEETS executes IMPORTRANGE not SCRIPTS. In Source sheet, range is selected to import to target (ie A1:G6)
counter=(counter-1)
}
}
searchFolder(); // After this function all file names are stored in variable `files`
timeStamp();
autoUpdate();
如果你也想记录文件夹中的所有文件,你可以执行Logger.log(files.join('\n'));
If you want to log all files in folder too, you can execute Logger.log(files.join('\n'));
这篇关于类型错误:在对象中找不到函数 hasNext(带有日期 ID 的文件夹)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!