Why is my .appendRow from Array adding an extra row? - google-apps-script

I pretty much have the Apps Script file working as intended. However, each time it is triggered, it also adds a second line where the sessionEmail is not captured and is blank.
function onEdit() {
var sessionEmail = Session.getActiveUser().getEmail();
var spreadsheetTimeZone = SpreadsheetApp.getActive().getSpreadsheetTimeZone();
var lastUpdatedString = Utilities.formatDate(new Date(), spreadsheetTimeZone, "MM/dd/yyyy' 'HH:mm:ss");
var s = SpreadsheetApp.getActiveSheet();
if (s.getName() == "Workload") { //checks that we're on the correct sheet
var r = s.getActiveCell();
if (r.getColumn() == 14) { //checks the column
var status = r.getValue();
var note = r.offset(0, -1);
var noteValue = note.getValue()
var delivery = r.offset(0, -5);
var deliveryValue = delivery.getValue()
}
// Validating fields are filled in
if (status == "Complete") {
var ui = SpreadsheetApp.getUi();
if (noteValue == '') { // if no note is entered, stop script with message box
var noStatus = ui.alert(
'Warning!',
'Please enter notation before choosing Complete.',
ui.ButtonSet.OK);
r.setValue('')
return;
}
// get destination range
var array = [lastUpdatedString, sessionEmail, deliveryValue, noteValue]
var ss = SpreadsheetApp.getActiveSpreadsheet();
var pasteSheet = ss.getSheetByName("Historical Notes Sheet");
pasteSheet.appendRow(array)
// clear response row
note.setValue('')
r.setValue('')
}
}
}
Image of what the results look like:
If any of you have any ideas on how to resolve this, and only append 1 line that has all the values, I would really appreciate it.
Thanks!
Update:
Logging the variable produces the following (expected) results, where the e-mail address appeared in the string.
Results still populating 2 rows:

Thanks for helping me troubleshoot.
I went into View>Executions, and noticed that the script was running twice each time at about 2 milliseconds apart. I think the function name onEdit() was acting like an On Edit trigger, and causing the script to run with another On Edit trigger that I had set up for it.
I tried removing the trigger, and leaving it with the onEdit()name, but that was causing it to not grab the sessionEmail. Changing the code to the below, and adding the trigger back causes the code to run as expected.
function appendLine() {
var sessionEmail = Session.getActiveUser().getEmail().toString();
var spreadsheetTimeZone = SpreadsheetApp.getActive().getSpreadsheetTimeZone();
var lastUpdatedString = Utilities.formatDate(new Date(), spreadsheetTimeZone, "MM/dd/yyyy' 'HH:mm:ss");
var s = SpreadsheetApp.getActiveSheet();
if (s.getName() == "Workload") { //checks that we're on the correct sheet
var r = s.getActiveCell();
if (r.getColumn() == 14) { //checks the column
var status = r.getValue();
var note = r.offset(0, -1);
var noteValue = note.getValue()
var delivery = r.offset(0, -5);
var deliveryValue = delivery.getValue().toString();
}
// Validating fields are filled in
if (status == "Complete") {
var ui = SpreadsheetApp.getUi();
if (noteValue == '') { // if no note is entered, stop script with message box
var noStatus = ui.alert(
'Warning!',
'Please enter notation before choosing Complete.',
ui.ButtonSet.OK);
r.setValue('')
return;
}
// get destination range
var array = [lastUpdatedString, sessionEmail, deliveryValue, noteValue]
var ss = SpreadsheetApp.getActiveSpreadsheet();
var pasteSheet = ss.getSheetByName("Historical Notes Sheet");
pasteSheet.appendRow(array)
// clear response row
note.setValue('')
r.setValue('')
}
}
}
Thanks again to everyone for your help!

Related

How to send calendar based when checkbox is ticked

right now , im having trouble to figuring out a script where when user ticks the checkbox, it will send an email invite 3 weeks in advance based on the payment date
would need your help.
Here is my code which is rather incomplete.
function sendreminder(){
var sheet = SpreadsheetApp.getActiveSheet();
var sheetName = sheet.getName();
var range = e.range;
var approvalEdit = range.getValue().toString(); // Use string to avoid accidentally accepting truthy values.
var column = range.getColumn();
var emailsend = "EMAIL_SENT";
var approvalColumnNo = 12;
var invoice = sheet.getRange(e.range.getRow(),12).getValue();
var calend = CalendarApp.createAllDayEvent();
if( sheetName === "Sheet1" && column === approvalColumnNo && approvalEdit === "true" ){
calend.createAllDayEvent('TEST', new Date('November 20, 2022')
SpreadsheetApp.flush();
Many thanks!
it will send an email invite 3 weeks in advance based on the payment date
You can try the following script:
function calendarEvent(e) {
var sheet = SpreadsheetApp.getActiveSheet();
var sheetName = sheet.getName();
var val = e.value;
var rCol = e.range.getColumn();
var rRow = e.range.getRow();
if(rCol==12 && val=="TRUE" && sheetName=="Sheet1")
{
var val = sheet.getRange(rRow, 10).getValue();
var day = new Date(val).getTime() + 86400000*21; // 21 for the number of days
var nDay = new Date(day);
CalendarApp.createAllDayEvent('This is a test event',nDay);
}
}
Example:
References:
How to add days to date?
createAllDayEvent()

Multiple onEdit scripts but getting error on line 2

I can't figure out what I'm doing wrong. I'm still new at this, so there's plenty of places for mistakes. I'm trying to get two versions of the same script to run. When a box is checked, if it's in column F I want function 7R to run, but if the checked box is in column K, I want function 8R to run. I used threads with similar questions to try and combine the two scripts but keep getting an error in line 2.
There error I was getting is Missing ; before statement: Line 2
Here is what I have:
function onEdit(e) {
8R();
7R();
}
function 8R() {
//Get the sheet you want to work with.
var editrange = {
top : 2,
bottom : 260,
left : 11,
right : 11};
//getRow() and not getrow()
var thisrow = e.range.getRow();
if (thisrow < editrange.top || thisrow > editrange.bottom) return;
//getColumn() and not getcolumn()
var thiscolumn = e.range.getColumn();
if (thiscolumn < editrange.left || thiscolumn > editrange.right) return;
//Line that replaces the erroneous 'var ss = e.range.getSheet()';
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Responsible");
//Grab the entire Range, and grab whatever values you need from it. EX:
rangevalues
var range8 = sheet.getRange("K3:K90");
var range28 = sheet.getRange("M3:M90");
var range2values8 = range28.getValues();
var rangevalues8 = range8.getValues();
//Loops through range results
for (var i in rangevalues8) {
Logger.log("rangevalues8["+i+"]["+0+"] is:"+rangevalues8[i][0]);//Added
//Set the rules logic
if (rangevalues8[i][0] == true) { //Modified
//Set the cell
range2values8[i][0] += 1; //Directly add 1 to range2values
Logger.log(range2values8);//Added
}
}
//copy new information
var destination = ss.getSheetByName('Compiled Data');//whatever page
var destCell8 = destination.getRange("I3:I90");
destCell8.setValues(range2values8);
//clear checkboxes
var cleaning = ss.getSheetByName('Asset Bank');
var cleaningcell8 = cleaning.getRange("A3:A90").getValues();
range8.setValues(cleaningcell8);
}
function 7R() {
//Get the sheet you want to work with.
var editrange = {
top : 2,
bottom : 260,
left : 6,
right : 6};
//getRow() and not getrow()
var thisrow7 = e.range.getRow();
if (thisrow7 < editrange.top || thisrow7 > editrange.bottom) return;
//getColumn() and not getcolumn()
var thiscolumn7 = e.range.getColumn();
if (thiscolumn7 < editrange.left || thiscolumn7 > editrange.right) return;
//Line that replaces the erroneous 'var ss = e.range.getSheet()';
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Responsible");
//Grab the entire Range, and grab whatever values you need from it. EX:
rangevalues
var range7 = sheet.getRange("F3:F90");
var range27 = sheet.getRange("H3:H90");
var range2values7 = range27.getValues();
var rangevalues7 = range7.getValues();
//Loops through range results
for (var i in rangevalues7) {
Logger.log("rangevalues7["+i+"]["+0+"] is:"+rangevalues7[i][0]);//Added
//Set the rules logic
if (rangevalues7[i][0] == true) { //Modified
//Set the cell
range2values7[i][0] += 1; //Directly add 1 to range2values
Logger.log(range2values7);//Added
}
}
//copy new information
var destination = ss.getSheetByName('Compiled Data');//whatever page
var destCell7 = destination.getRange("I93:I180");
destCell7.setValues(range2values7);
//clear checkboxes
var cleaning = ss.getSheetByName('Asset Bank');
var cleaningcell7 = cleaning.getRange("A3:A90").getValues();
range7.setValues(cleaningcell7);
}
Here is a link to a replica sheet of what I'm working on with all of the relevant information: https://docs.google.com/spreadsheets/d/1PWaWm7AryljOMd5Aq1O2RSADyRFNVZbUDqKO__SzW2w/edit?usp=sharing
Javascript variables and functions names cannot start with numbers. Replace 8R and 7R with a valid identifier, for example func8R and func7R.
Apart from that, you 8R and 7R functions try to access right away a e.range variable, but that is not defined anywhere. I'm assuming you're trying to read the onEdit event parameter, but you have to pass that down onto your functions and also define the parameter their, e.g.
function onEdit(e) {
f8R(e);
f7R(e);
}
function f8R(e) {
//... continue (remember to also declare on f7R)
If you want to manually test this function you have to fill in this e parameter, as the environment would do when you actually change something on the spreadsheet. I like to do that by writing another "caller" function, like this:
function testOnEdit() {
onEdit({range: SpreadsheetApp.getActiveSheet().getRange('F2')});
}
Finally was able to get the script working. First, I changed the names as Henrique suggested. Then got the onEdit block sorted out. Thanks everyone for the help and advice. I'd still be angry at my machine without you all.
function onEdit(e) {
//don't need entire range, just the column that was modified to figure out which function to call
var editColumn = e.range.getColumn();
//confirm edit was a box being checked before running the code (as this seems to run on ANY sheet edit)
if (e.oldValue === "false" && e.value === "TRUE") {
if (editColumn === 6)
func7R(e);
else if (editColumn === 11)
func8R(e);
}
}
function func8R(e) {
//Get the sheet you want to work with.
var editRange = {
top : 2,
bottom : 260,
left : 11,
right : 11};
//getRow() and not getrow()
var thisRow = e.range.getRow();
if (thisRow < editRange.top || thisRow > editRange.bottom) return;
//getColumn() and not getcolumn()
var thisColumn = e.range.getColumn();
if (thisColumn < editRange.left || thisColumn > editRange.right) return;
//Line that replaces the erroneous 'var ss = e.range.getSheet()';
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Responsible");
//Grab the entire Range, and grab whatever values you need from it. EX: rangevalues
var range8 = sheet.getRange("K3:K90");
var range28 = sheet.getRange("M3:M90");
var range2values8 = range28.getValues();
var rangevalues8 = range8.getValues();
//Loops through range results
for (var i in rangevalues8) {
Logger.log("rangevalues8["+i+"]["+0+"] is:"+rangevalues8[i][0]);//Added
//Set the rules logic
if (rangevalues8[i][0] == true) { //Modified
//Set the cell
range2values8[i][0] += 1; //Directly add 1 to range2values
Logger.log(range2values8);//Added
}
}
//copy new information
var destination = ss.getSheetByName('Compiled Data');//whatever page
var destCell8 = destination.getRange("I3:I90");
destCell8.setValues(range2values8);
//clear checkboxes
var cleaning = ss.getSheetByName('Asset Bank');
var cleaningcell8 = cleaning.getRange("A3:A90").getValues();
range8.setValues(cleaningcell8);
}
function func7R(e) {
//Get the sheet you want to work with.
var editRange = {
top : 2,
bottom : 260,
left : 6,
right : 6};
//getRow() and not getrow()
var thisrow7 = e.range.getRow();
if (thisrow7 < editRange.top || thisrow7 > editRange.bottom) return;
//getColumn() and not getcolumn()
var thiscolumn7 = e.range.getColumn();
if (thiscolumn7 < editRange.left || thiscolumn7 > editRange.right) return;
//Line that replaces the erroneous 'var ss = e.range.getSheet()';
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Responsible");
//Grab the entire Range, and grab whatever values you need from it. EX: rangevalues
var range7 = sheet.getRange("F3:F90");
var range27 = sheet.getRange("H3:H90");
var range2values7 = range27.getValues();
var rangevalues7 = range7.getValues();
//Loops through range results
for (var i in rangevalues7) {
Logger.log("rangevalues7["+i+"]["+0+"] is:"+rangevalues7[i][0]);//Added
//Set the rules logic
if (rangevalues7[i][0] == true) { //Modified
//Set the cell
range2values7[i][0] += 1; //Directly add 1 to range2values
Logger.log(range2values7);//Added
}
}
//copy new information
var destination = ss.getSheetByName('Compiled Data');//whatever page
var destCell7 = destination.getRange("I93:I180");
destCell7.setValues(range2values7);
//clear checkboxes
var cleaning = ss.getSheetByName('Asset Bank');
var cleaningcell7 = cleaning.getRange("A3:A90").getValues();
range7.setValues(cleaningcell7);
}

Google Apps Script - Append Array of Variables to Sheet

I've got the following code that grabs all of the information I need form the active line, and puts them into an array of variables. However, I can't seem to find good documentation on how to append the variables to a another sheet in the same document:
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
var spreadsheetTimeZone = SpreadsheetApp.getActive().getSpreadsheetTimeZone();
var lastUpdatedString = Utilities.formatDate(new Date(), spreadsheetTimeZone, "MM/dd/yyyy' 'HH:mm:ss");
var sessionEmail = Session.getActiveUser().getEmail();
if (s.getName() == "Workload") { //checks that we're on the correct sheet
var r = s.getActiveCell();
if (r.getColumn() == 14) { //checks the column
var status = r.getValue();
var note = r.offset(0, -1);
var noteValue = note.getValue()
var delivery = r.offset(0, -5);
var deliveryValue = delivery.getValue()
}
var array = [lastUpdatedString, sessionEmail, deliveryValue, noteValue]
// get destination range
var ss = SpreadsheetApp.getActiveSpreadsheet();
var pasteSheet = ss.getSheetByName("Historical Notes Sheet");
var destination = pasteSheet.getRange(pasteSheet.getLastRow() + 1, 1, 1, 1);
}
}
I'd like to append the values from var array into the "Historical Notes Sheet", in the same order they are in the array.
Any help/advice you all could provide would be greatly appreciated.
Thanks!
Try this
pasteSheet.appendRow(array);
More info https://developers.google.com/apps-script/reference/spreadsheet/sheet#appendRow(Object)

One trigger to activate two functions

Self taught noob, working in google sheets, sorry in advance for the mess here. I am trying to pull data from two cells into a new sheet. Both of these are pulled in after I trigger them with an onEdit. I need to enter a value and hit enter on both cells for them to be placed into the new sheet. How would I set this up so that when I trigger "D9" I would pull in "C9" plus "D9"?
function onEdit(e){
// Set a comment on the edited cell to indicate when it was changed.
if(e.range.getA1Notation() === 'C9'){
var input = e.range.getValue();
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var inputSheet = sheet.getSheetByName("input")
var masterSheet = sheet.getSheetByName("master inventory");
var currentRow = inputSheet.getRange("E9").getValue();
masterSheet.getRange("A3:A1000").getCell(currentRow, 1).setValue(input);
inputSheet.getRange("E9").setValue(currentRow, 1);
}
if(e.range.getA1Notation() === 'D9'){
var input = e.range.getValue();
e.range.clearContent();
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var inputSheet = sheet.getSheetByName("input")
var masterSheet = sheet.getSheetByName("master inventory");
var currentRow = inputSheet.getRange("E9").getValue();
masterSheet.getRange("B3:B1000").getCell(currentRow, 1).setValue(input);
inputSheet.getRange("E9").setValue(currentRow + 1);
}
}
I am not sure exactly what you are trying to do, but try this:
function onEdit(e) {
var x=e.source.getSheetName()
if( x == "input" ) { //checks that we're on the correct sheet
var y = e.range.getSheet().getActiveCell();
var z= e.range.getSheet().getActiveCell().getA1Notation()
var row=e.range.getSheet().getActiveCell().getRow()
if(z=="D9" && x=="input"){
var cell=e.range.getSheet().getActiveCell().getValue()
e.range.getSheet().getActiveCell().clearContent()
var nextCell =e.range.getSheet().getActiveCell().offset(0,-1).getValue()
e.range.getSheet().getActiveCell().offset(0,-1).clearContent()
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var masterSheet = sheet.getSheetByName("master inventory");
masterSheet.getRange("A3:A1000").getCell(row-2, 1).setValue(nextCell);
masterSheet.getRange("B3:B1000").getCell(row-2, 1).setValue(cell);
}}}

Problems with getting reading a value of a range to work correctly

I'm working on an onEdit event that would detect changes made in specific columns. I want to then send an email notification if the change is to a specific value.
So far I got this:
function sendNotification(e)
{
// static values
var sheetNameToWatch = "Category";
var orderStatusColToWatch = 13;
var processingStatusColToWatch = 52;
var valueToWatch1 = "In Game";
var columnOfEditedOrder = 3;
//dynamic values
var ss = e.source;
var range = e.range;
var sheet = e.range.getSheet(); //works
var sheetName = sheet.getName(); //works
var rowOfEditedOrder = range.getRow(); //works
var nameOfEditedItem = getRange(rowOfEditedCard,3).getValue();
Logger.log(nameOfEditedCard);
if (sheetName == sheetNameToWatch && range.getColumn() ==
processingStatusColToWatch)
{
if (value == valueToWatch1)
{
MailApp.sendEmail("everyday#normalemail.com", "Example topic", "Value for item "+nameOfEditedItem+" has been updated to "+valueToWatch1+".");
}
}
else Logger.log("Something went wrong");
}
Now, the issue I have is that the variable nameOfEditedItem is not set properly to the right value, even though rowOfEditedOrder returns correct value. Whenever I use the logger to log the nameOfEditedItem value for me, the logger does not return anything.
I would appreciate the help with this one.
Thanks
Your code has a bad line
var nameOfEditedItem = getRange(rowOfEditedCard,3).getValue();
You forgot the sheet object, so you do not have a range to work with and thus you do not have a value. You probably meant to have
var nameOfEditedItem = sheet.getRange(rowOfEditedCard,3).getValue();