Reputation: 695
Can any help with the following issue??? Pretty new to app script / javascript and would appreciate any help or guidance to figure out..
TypeError: Cannot find function offset in object Timestamp, (then continues listing the column headings in the red warning banner.
function uiSendLogEmail() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var data = sheet.getDataRange().getValues();
data = data.offset(1,0,data.getNumRows())-1;
// For Loop
for ( var i = 0; i < data.length; i++ ) {
var row = data[i];
var approved = row[5];
var sentEmail = row[6];
var snapshot = row[3];
//if stmt in For Loop
if ( approved != "Yes" ) {
data[i][5] = "Yes";
data.getDataRange().setValues(data);
}// if stmt end curly
else if ( approved == "Yes" && sentEmail != "Yes" ) {
data[i][6] = "Yes";
data.getDataRange().setValues(data);
GmailApp.sendEmail("[email protected]", "subject", "body" + "whatever " + snapshot);
}//else if end curly
else {
return;
}//else stmt end curly
}// for loop end curly
}
Upvotes: 0
Views: 2155
Reputation: 37269
I made a few basic tweaks that will hopefully point you in the right direction (and thanks to @AdamL for the .shift()
method - much better than what I had in there before :) ):
function uiSendLogEmail() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var data = sheet.getDataRange().getValues();
// Move the values down
data.shift();
// For Loop
for ( var i = 0; i < data.length; i++ ) {
var row = data[i];
var approved = row[5];
var sentEmail = row[6];
var snapshot = row[3];
// Here we set a range equal to the data range offset by 1 + our current
// position in the loop (cycling through rows), and then get the A1 notation
// of the first row, which we use to get that particular range and prep
// it for adding values
var rng = sheet.getDataRange().offset(i+1,0,1).getA1Notation();
myRange = sheet.getRange(rng);
//if stmt in For Loop
if ( approved != "Yes" ) {
// Here we can just work with the row element itself
row[5] = "Yes";
// Because setValues expects a two dimensional array,
// we wrap our row in brackets to effectively convert it to one
myRange.setValues([row]);
} // if stmt end curly
else if ( approved == "Yes" && sentEmail != "Yes" ) {
// Same here as above
row[6] = "Yes";
myRange.setValues([row]);
GmailApp.sendEmail("[email protected]", "subject", "body" + "whatever " + snapshot);
}
else {
return;
}
}
Upvotes: 1
Reputation: 24619
I think you are wanting to remove the first row of data (the headers); so if that's the case, try replacing:
data = data.offset(1,0,data.getNumRows())-1;
with
data.shift();
Upvotes: 0