1

I'm trying to create a google script that counts the number of files in a folder, then assigns this number to a variable that can be used to define the size of an array.

  1. ID folder
  2. get files in folder
  3. set counter = filecount
  4. use counter to define array size

my attempt:

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); 

I'm expecting to create an array with a size equal to the number of files in the folder, but I don't believe this is what is happening.

The script runs, there is more after this I'm leaving out for simplicity. I think the problem may be that I cannot use the variable count in new Array(), is that correct?


Here is the whole script:

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);
  • Why you "don't believe"? Are you getting an error message? – Rubén Dec 07 '17 at 16:50
  • Possible duplicate of [How do array sizes work in Javascript](https://stackoverflow.com/questions/6232151/how-do-array-sizes-work-in-javascript) – Rubén Dec 07 '17 at 16:50
  • No error message, but I'm writing out the values from the nested array, and all it writes is "not found", so the dimension wasn't being input at all I don't think. – Arlen Spexarth Dec 08 '17 at 13:15

1 Answers1

0

You can use the length property to set the number of elements in an array:

var nesteddata = new Array(); 

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

The array will look like this:

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

It will have the number of elements in it, with all the elements having a null value

Alan Wells
  • 30,746
  • 15
  • 104
  • 152
  • That seems to work, I'm having a similar issue now, I need to use the length of the count variable to define the range we write out to: sheettoupdate.getRange(2,2,110,158).setValues(nesteddata); – Arlen Spexarth Dec 08 '17 at 13:08
  • I used nesteddata.length in place of 110, seems to work. The script runs, but it does not pull any data. But the dimensions of the array are correct (equal to the number of files). But the array is filled with "NOT_FOUND". – Arlen Spexarth Dec 08 '17 at 13:23
  • I am guessing it may have to do with 2 while loops not executing as I would like, perhaps the second while loop is not running? – Arlen Spexarth Dec 08 '17 at 13:46
  • Ok, ssfiles needs to be split into 2 variables, as we run a hasnext on one of them, we need a second for the second while loop. Many thanks for your help, it's solved. – Arlen Spexarth Dec 08 '17 at 14:04