Google Apps Script - Append Array of Variables to Sheet - google-apps-script

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)

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

Trying to convert Google Sheets moveTo script to copyTo script

In my small department, we currently have a simplistic ticketing system where a user fills out a google form to request service and then the ticket is added to a google sheet which we keep up on our work PCs and displays in our offices.
I'm attempting to make a script where when a checkbox is clicked, a timestamp is created on a certain column and then row on one sheet is moved over to another sheet where "Completed Tickets" are archived.
I've been able to get the script working completely with the timestamp, and I have been able to get the moving portion working completely with a moveTo function, but ONLY when you manually type in the ticket. If the ticket is submitted using Google forms, it blocks the use of the moveTo command, because it will only allow you to copy and paste formdata, and not cut and paste.
So, what I'm attempting to do is convert my 2nd onEdit script to a copyTo, rather than a moveTo. However, after many attempts at this, I can't seem to get it to function. I've posted the script I have below. Any help would be appreciated.
function onEdit(event) {
myFunction1(event);
}
function myFunction1(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
var timezone = "GMT-7";
var timestamp_format = "M-d-yyyy HH:MM:SS"; // Timestamp Format.
var updateColName = "Timestamp";
var timeStampColName = "Completed";
var sheet = event.source.getSheetByName('Form Responses 1'); //Name of the sheet where you want to run this script.
var actRng = event.source.getActiveRange();
var editColumn = actRng.getColumn();
var index = actRng.getRowIndex();
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var dateCol = headers[0].indexOf(timeStampColName);
var updateCol = headers[0].indexOf(updateColName); updateCol = updateCol+1;
if(s.getName() == "Form Responses 1" && r.getColumn() == 11 && r.getValue() == true) {
var cell = sheet.getRange(index, dateCol + 1);
var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
cell.setValue(date);
} //closes if statement operations; no close to myFunction1()
function myFunction2() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Form Responses 1" && r.getColumn() == 11 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Completed Tickets");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
} //closes if statement operation
} //closes myFunction2()
myFunction2(); //calls myFunction2() so that it will run after myFunction1() code
} // closes myFunction1()
You can copy row from response sheet to target sheet then delete row from response sheet.
if (s.getName() == "Form Responses 1" && r.getColumn() == 11 && r.getValue() == true) {
var cell = sheet.getRange(index, dateCol + 1);
var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
cell.setValue(date);
// copy n delete
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Completed Tickets");
targetSheet.getRange(targetSheet.getLastRow() + 1, 1, 1, numColumns).setValues(s.getRange(row, 1, 1, numColumns).getValues());
s.deleteRow(row);
}

Why is my .appendRow from Array adding an extra row?

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!

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

script to automatically add a new row when filling the field of column 8

I need a script to automatically add a blank line at the beginning whenever I finish filling the field in column 8 of a line?
Note: It is for this new line preserve the formula of the previous line.
This is the script that I am using.
It works by writing any part of the line (any column)
but I wanted him to be activated only when i write something in column 8.
function onEdit(e) {
if (e) {
var ss = e.source.getActiveSheet();
var r = e.source.getActiveRange();
// If you want to be specific
// do not work in first row
// do not work in other sheets except "Outubro"
if (r.getRow() != 1 && ss.getName() == "Outubro") {
// E.g. status column is
status = ss.getRange(r.getRow(), 8).getValue();
// Status
if (status == 'yes', 'null') {
var planilha = SpreadsheetApp.getActiveSpreadsheet();
var folha = planilha.getActiveSheet();
if (folha.getName() === "Outubro")
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
sheet.insertRowBefore(4);
var rangeToCopy = sheet.getRange(3, 1, 1, sheet.getMaxColumns());
rangeToCopy.copyTo(sheet.getRange(4, 1));
}
}
}
}
Thank you for the help from the community.
The code is now working.
I'll leave the code here for anyone who need.
Once again thank you all for the help.
function onEdit(e) {
if (e) {
var ss, activeRange, activeColumn;
var planilha = SpreadsheetApp.getActiveSpreadsheet();
var folha = planilha.getActiveSheet();
if (folha.getName() === "Outubro")
ss = SpreadsheetApp.getActiveSpreadsheet();
activeRange = ss.getActiveRange();
activeColumn = activeRange.getColumn();
//Logger.log('activeColumn: ' + activeColumn); //VIEW LOGS
if (activeColumn !== 8) {return;};//Quit if not column 8 {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
sheet.insertRowBefore(4);
var rangeToCopy = sheet.getRange(3, 1, 1, sheet.getMaxColumns());
rangeToCopy.copyTo(sheet.getRange(4, 1));
}
}