I have the below script which i thought was running but when I tested just now I got the error
TypeError: Cannot read property "0" from undefined line 31.
I have bolded it below. but it is this line. if(dataA[j][0] && dataA[j][1])
. I have a =today()
in A2 to get the date that the script uses to enter in the archive sheet, so not sure if this is the issue as it is using the google sheet format "date" in the cell.
function dataBackup()
{
var inputSS = SpreadsheetApp.getActiveSpreadsheet();
var archiveSS = SpreadsheetApp.openById('1uPA_4CRmv2wkqe1uljViJ-
1z1NTNDmwS3aE6TDowe88');
var sheetNames = ['AM trip', 'PM trip', 'Pool / Beach', 'Night Dive'];
for (var i=0; i<sheetNames.length; i++)
{
var inputSheet = inputSS.getSheetByName(sheetNames[i]);
var archiveSheet = archiveSS.getSheetByName(sheetNames[i]);
var date=new Date(inputSheet.getRange('A2').getValue());
var rng=inputSheet.getRange('E7:U37');
var dataA=rng.getValues();
var data=[];
for(var j=0;i<dataA.length;j++)
{
**if(dataA[j][0] && dataA[j][1])**
{
data.push([dataA[j].splice(0,0,date)]);
}
}
var dv=archiveSheet.getRange(archiveSheet.getLastRow(), 1).getValue();
date=(typeof(dv)!='undefined')?dv:'No Date Found';
if (data.length>0)
{
archiveSheet.insertRowsAfter(archiveSheet.getLastRow(), data.length);
archiveSheet.getRange(archiveSheet.getLastRow() + 1, 1, data.length, rng.getWidth()+1).setValues(data);
}
else
{
archiveSheet.insertRowsAfter(archiveSheet.getLastRow(), 1);
archiveSheet.getRange(archiveSheet.getLastRow() + 1, 1, 1, 2).setValues([[date, 'No Data']]);
}
}
}
Short answer
There is a typo on the related for loop. On
replace
i
byj
Extendet answer
Regarding if =TODAY() is causing the error, it isn't, but it's worth to note that TODAY() returns today's date. Getting a date value by using getValue() a date object is passed to the Google Apps Script engine, so instead of
try