设置数组大小等于文件夹

时间:2017-12-07 16:20:34

标签: javascript arrays google-apps-script google-sheets

我尝试创建一个计算文件夹中文件数的Google脚本,然后将此数字分配给可用于定义数组大小的变量。

  1. ID文件夹
  2. 获取文件夹
  3. 中的文件
  4. set counter = filecount
  5. 使用计数器来定义数组大小
  6. 我的尝试:

    function setarraytofilecount() {
    var dealspsfolderid = "ID goes here";
    var dealspsfolder = DriveApp.getFolderById(dealspsfolderid);
    var ssfiles = dealspsfolder.getFiles();
    
    var count, file;
    
    count = 0;
    
    while (ssfiles.hasNext()) {
    count++;
    file = ssfiles.next();
    }
    
    var nesteddata = new Array(count); 
    

    我希望创建一个大小等于文件夹中文件数量的数组,但我不相信这就是正在发生的事情。

    脚本运行,在此之后还有更多内容为了简单起见而离开了。我认为问题可能是我不能在新的Array()中使用变量count,这是正确的吗?

    以下是整个脚本:

    function autoUpdateFiles() {
    
    // set the folder to pull files from
    var dealspsfolderid = "removed";
    var dealspsfolder = DriveApp.getFolderById(dealspsfolderid);
    var ssfiles = dealspsfolder.getFiles();
    var trackerurl = "removed";
    var sstoupdate = SpreadsheetApp.openByUrl(trackerurl);
    
    // set the sheet to update
    var sheettoupdate = sstoupdate.getSheetByName('AutoTracker');
    
    // indicates current deal row
    var rowcounter = 0;
    
    // get number of files in the folder which we can use to define array size
    var count,file;//Define variables without assigning a value
    count = 0;
    while (ssfiles.hasNext()) {
     count++;
     file = ssfiles.next();
    }
    
    // nested array of row data arrays. i.e. each index is an array of a row 
    var nesteddata = new Array(); 
    nesteddata.length = count;
    for(i = 0; i < nesteddata.length; i++){
      nesteddata[i] = new Array(158);
    }
    
    // nested array of row data arrays. i.e. each index is an array of a row 
    var URLarray = new Array(); 
    URLarray.length = count;
    for(i = 0; i < URLarray.length; i++){
     URLarray[i] = new Array(1);
    }
    
    // iterate through each file
    while (ssfiles.hasNext()) {
    
    // pull the next file
    var file = ssfiles.next();
    
    // get the url       
    Logger.log(file.getUrl());
    
    // activate the sheet
    var spreadsheet = SpreadsheetApp.open(file);
    var activesheet = spreadsheet.getSheets()[0].activate();
    
    // pull values from the files 
    // ranges are ROW, COLUMN
    var URL = file.getUrl();  
    var colC = activesheet.getRange(3, 3, 158 ,3).getValues();           
    
    // transition the vertical multidimensional array returned into flat array for a row
    for(var i = 0; i < colC.length; i++){
       nesteddata[rowcounter][i]=colC[i][0];
      }
    
     for(var i = 0; i < URLarray.length; i++){
       URLarray[rowcounter]= [URL];    
      }
    rowcounter ++;
    }
    
    // write out to the sheet  
    sheettoupdate.getRange(2,1,nesteddata.length,1).setValues(URLarray);
    sheettoupdate.getRange(2,2,nesteddata.length,158).setValues(nesteddata);
    

1 个答案:

答案 0 :(得分:0)

您可以使用length属性设置数组中元素的数量:

var nesteddata = new Array(); 

nesteddata.length = count;
Logger.log(nesteddata);
Logger.log(nesteddata.length);

数组将如下所示:

[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null]

它将包含元素数量,所有元素都具有null