TypeError:在对象中找不到函数hasNext(带有日期ID的文件夹)

时间:2016-07-14 17:07:42

标签: javascript google-apps-script typeerror

我是javascript和谷歌应用程序编码的新手。我制作的数据库会在每晚自动将新用户更新为聚合表。

我的searchfolder()函数不会执行,因为我一直收到错误" TypeError:找不到对象中的函数hasNext Thu Jul 14 09:48:01 PDT 2016 INFO:Target。 (第24行,每天工作)"

var文件在DriveApp.getFolderById(folderId)之后有.getfiles所以我没有想到问题是什么

CODE:

alt

1 个答案:

答案 0 :(得分:0)

您声明变量files两次。此外,推入日志然后获取日志是不好的做法。更好地使用数组。您收到错误,因为存储文件名的变量files在第一次循环后更改为日志上下文,这意味着hasNext对象中没有更多函数files(它不是类型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'));