I am new to scripting, and have been trying to get a script to place a date in an adjacent cell when a checkbox is checked. This is a minor part of the whole script I am trying to get working. I am trying to write a script to build a new sheet from another sheets values, including naming the sheet.
I can get the script to work when I type the sheet name into the if statement, but when I try and use an argument by calling the sheet name and calling string, I can not get them to return a true value.
When I log the sheet name and the string they match. If I copy and paste ether value from the log into the sheet name it will then return true. It will also return true when I type the name of the sheet.
I have tried to change the type using: toString() but this has not worked.
I have tried to use the length and typeof to find the solution, from this page:
How to debug identical strings that do not equal in google app script?
But they where the same type and length. I have tried to get the every function on this page to work but have been unsuccessful in getting it to run.
I have used it like this:
[td].every((char,i) => char === nac[i] || console.info(`Unequal character at ${i}`))
and get this error:
10:55:03 AM Error
TypeError: Cannot read property 'every' of undefined
onEdit # Time Stamp test day.gs:33
Code:
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ac = ss.getActiveSheet();
var as = ss.getActiveSheet().getSheetName();
var d = new Date();
var month = new Array(12);
month[0] = "Jan";
month[1] = "Feb";
month[2] = "Mar";
month[3] = "Apl";
month[4] = "May";
month[5] = "Jun";
month[6] = "Jul";
month[7] = "Aug";
month[8] = "Sep";
month[9] = "Oct";
month[10] = "Nov";
month[11] = "Dec";
var year = d.getFullYear();
var date = d.getDate();
var today = month[d.getMonth()] + " " + date +", " + year
var td = "Test " + today
var nac = ac.getSheetName()
Logger.log(td)
Logger.log(nac)
Logger.log(nac == td)
if(nac == td) {
var cell = ac.getActiveCell()
var col = cell.getColumn()
if(col == 9 && cell.getValue() == true){
Logger.log(123456)
var date = new Date()
var dateCell = cell.offset(0,1)
dateCell.setValue(date)
}//close if
} // Close if
} // Close function
Log
11:26:46 PM Notice Execution started
11:26:47 PM Info Test Dec 27, 2020
11:26:47 PM Info Test Dec 27, 2020
11:26:47 PM Info false
11:26:47 PM Notice Execution completed
Related
I'm building a script to retrieve certain events from my calendar and send an email for each qualifying event with a hyperlink to the event. For that I need event id, not icalUID. How do I get that? Here is my code (actual IDs and names were removed):
function GetFamilyEvents () {
//Gets all events that start/end/or span within next 30 days
var FamilyCalendar = CalendarApp.getCalendarById("someID#group.calendar.google.com");
var CurrentDate = new Date(); //Gets current date
var RangeEnd = new Date(CurrentDate.getTime() + 720 * 60 * 60 * 1000); //Adds 30 days in milliseconds to the current date
var FamilyEvents = FamilyCalendar.getEvents(CurrentDate,RangeEnd); //returns events that start, end, or encompass wihtin 30 days starting from today; Time range is Current Date to Range End
for(var i = 0; i<FamilyEvents.length; i++){
var EventTitle = FamilyEvents[i].getTitle();
var EventCreatedDate = FamilyEvents[i].getDateCreated();
var EventStartDate = FamilyEvents[i].getStartTime();
var EventEndDate = FamilyEvents[i].getEndTime();
var EventCreator = FamilyEvents[i].getCreators(); //Gets the creator of the event to email notificaiton to
var EventID = FamilyEvents[i].getID();
var CalendarID = FamilyCalendar.getId();
//Check if an event was created today AND does not have our names or "FYI" in its title
if(EventCreatedDate.valueOf() <= CurrentDate.valueOf() && EventTitle.indexOf('Name1')<0 && EventTitle.indexOf('Name2')<0 && EventTitle.indexOf('Name3')<0 && EventTitle.indexOf('Name4')<0 && EventTitle.indexOf('Name5')<0 && EventTitle.indexOf('FYI')<0)
{
//Creates variables for the HTML body of the email notification. The same variables are referenced in the body of the HTML template.
var EmailMessage = HtmlService.createTemplateFromFile("EmailMessage"); //"EmailMessage" is the name of the HTML file in this script.
EmailMessage.recepient = EventCreator;
EmailMessage.eventTitle = EventTitle;
EmailMessage.eventStartDate = EventStartDate;
EmailMessage.eventEndDate = EventEndDate;
EmailMessage.calendarID = CalendarID;
EmailMessage.eventID = EventID;
};
Thank you
From For that I need event id, not icalUID., when you want to retrieve the event ID, how about the following modification?
From:
var EventID = FamilyEvents[i].getID();
To:
var EventID = FamilyEvents[i].getId().split("#")[0];
Id of getId() is required to be modifieid.
getId() returns the value like ####google.com which is iCalUID. The event ID is retrieved from this as ###.
Reference:
getId()
Added:
From your replying,
what I meant was that my original code and your modifications give the same result: 2C18CFDB-E6B6-4653-AB65-21C990969103 - this is what I get for both options.
In this case, how about using Calendar API? By this, both the event ID and also the hyperlink of the event can be retrieved. When this is reflected in your script, it becomes as follows.
Sample script:
Before you use this, please enable Calendar API at Advanced Google services. And, please set your calendar ID.
function sample() {
var calendarId = "someID#group.calendar.google.com"; // Please set your calendar ID
//Gets all events that start/end/or span within next 30 days
var CurrentDate = new Date(); //Gets current date
var RangeEnd = new Date(CurrentDate.getTime() + 720 * 60 * 60 * 1000); //Adds 30 days in milliseconds to the current date
var items = Calendar.Events.list(calendarId, { timeMin: CurrentDate.toISOString(), timeMax: RangeEnd.toISOString(), maxResults: 2500 }).items;
items.forEach(e => {
var EventTitle = e.summary; // FamilyEvents[i].getTitle();
var EventCreatedDate = new Date(e.created); // FamilyEvents[i].getDateCreated();
var EventStartDate = new Date(e.start.dateTime || e.start.date);// FamilyEvents[i].getStartTime();
var EventEndDate = new Date(e.end.dateTime || e.end.date); // FamilyEvents[i].getEndTime();
var EventCreator = e.creator.email; // FamilyEvents[i].getCreators(); //Gets the creator of the event to email notificaiton to
var EventID = e.id; // FamilyEvents[i].getID();
var CalendarID = calendarId; // FamilyCalendar.getId();
var eventUrl = e.htmlLink; // If you want to retrieve the hyperlink of the event, you can use this.
//Check if an event was created today AND does not have our names or "FYI" in its title
if (EventCreatedDate.valueOf() <= CurrentDate.valueOf() && EventTitle.indexOf('Name1') < 0 && EventTitle.indexOf('Name2') < 0 && EventTitle.indexOf('Name3') < 0 && EventTitle.indexOf('Name4') < 0 && EventTitle.indexOf('Name5') < 0 && EventTitle.indexOf('FYI') < 0) {
//Creates variables for the HTML body of the email notification. The same variables are referenced in the body of the HTML template.
var EmailMessage = HtmlService.createTemplateFromFile("EmailMessage"); //"EmailMessage" is the name of the HTML file in this script.
EmailMessage.recepient = EventCreator;
EmailMessage.eventTitle = EventTitle;
EmailMessage.eventStartDate = EventStartDate;
EmailMessage.eventEndDate = EventEndDate;
EmailMessage.calendarID = CalendarID;
EmailMessage.eventID = EventID;
// do something
};
});
}
In this sample script, eventUrl is the hyperlink of the event.
The Google Event ID can be determined if you have both the iCalUID and the corresponding CalendarID that event belongs to. And once you have the Event ID, assembling a URL for the event is a piece of cake.
Step 1
Grab the first part of the iCalUID.. up to but not including the # sign.
Step 2
Concatenate the string from step 1 with a the CalendarID separated by a single space.
Step 3
Use the built-in Utilities class to encode the string from step 2 to a web-safe base64 string.
Step 4
Assemble your url
let str = EventID.split('#')[0].toString();
let str2 = str + ' ' + CalendarID;
let eid = Utilities.base64EncodeWebSafe(str2, Utilities.Charset.UTF_8);
let url = 'https://www.google.com/calendar/event?eid=' + eid;
We can simplify all that into a one liner. Add it to your for loop after the CalendarID variable declaration.
let url = 'https://www.google.com/calendar/event?eid=' + Utilities.base64EncodeWebSafe(EventID.split('#')[0].toString() + ' ' + CalendarID, Utilities.Charset.UTF_8);
I'm building a script to retrieve certain events from my calendar and send an email for each qualifying event with a hyperlink to the event. For that I need event id, not icalUID. How do I get that? Here is my code (actual IDs and names were removed):
function GetFamilyEvents () {
//Gets all events that start/end/or span within next 30 days
var FamilyCalendar = CalendarApp.getCalendarById("someID#group.calendar.google.com");
var CurrentDate = new Date(); //Gets current date
var RangeEnd = new Date(CurrentDate.getTime() + 720 * 60 * 60 * 1000); //Adds 30 days in milliseconds to the current date
var FamilyEvents = FamilyCalendar.getEvents(CurrentDate,RangeEnd); //returns events that start, end, or encompass wihtin 30 days starting from today; Time range is Current Date to Range End
for(var i = 0; i<FamilyEvents.length; i++){
var EventTitle = FamilyEvents[i].getTitle();
var EventCreatedDate = FamilyEvents[i].getDateCreated();
var EventStartDate = FamilyEvents[i].getStartTime();
var EventEndDate = FamilyEvents[i].getEndTime();
var EventCreator = FamilyEvents[i].getCreators(); //Gets the creator of the event to email notificaiton to
var EventID = FamilyEvents[i].getID();
var CalendarID = FamilyCalendar.getId();
//Check if an event was created today AND does not have our names or "FYI" in its title
if(EventCreatedDate.valueOf() <= CurrentDate.valueOf() && EventTitle.indexOf('Name1')<0 && EventTitle.indexOf('Name2')<0 && EventTitle.indexOf('Name3')<0 && EventTitle.indexOf('Name4')<0 && EventTitle.indexOf('Name5')<0 && EventTitle.indexOf('FYI')<0)
{
//Creates variables for the HTML body of the email notification. The same variables are referenced in the body of the HTML template.
var EmailMessage = HtmlService.createTemplateFromFile("EmailMessage"); //"EmailMessage" is the name of the HTML file in this script.
EmailMessage.recepient = EventCreator;
EmailMessage.eventTitle = EventTitle;
EmailMessage.eventStartDate = EventStartDate;
EmailMessage.eventEndDate = EventEndDate;
EmailMessage.calendarID = CalendarID;
EmailMessage.eventID = EventID;
};
Thank you
From For that I need event id, not icalUID., when you want to retrieve the event ID, how about the following modification?
From:
var EventID = FamilyEvents[i].getID();
To:
var EventID = FamilyEvents[i].getId().split("#")[0];
Id of getId() is required to be modifieid.
getId() returns the value like ####google.com which is iCalUID. The event ID is retrieved from this as ###.
Reference:
getId()
Added:
From your replying,
what I meant was that my original code and your modifications give the same result: 2C18CFDB-E6B6-4653-AB65-21C990969103 - this is what I get for both options.
In this case, how about using Calendar API? By this, both the event ID and also the hyperlink of the event can be retrieved. When this is reflected in your script, it becomes as follows.
Sample script:
Before you use this, please enable Calendar API at Advanced Google services. And, please set your calendar ID.
function sample() {
var calendarId = "someID#group.calendar.google.com"; // Please set your calendar ID
//Gets all events that start/end/or span within next 30 days
var CurrentDate = new Date(); //Gets current date
var RangeEnd = new Date(CurrentDate.getTime() + 720 * 60 * 60 * 1000); //Adds 30 days in milliseconds to the current date
var items = Calendar.Events.list(calendarId, { timeMin: CurrentDate.toISOString(), timeMax: RangeEnd.toISOString(), maxResults: 2500 }).items;
items.forEach(e => {
var EventTitle = e.summary; // FamilyEvents[i].getTitle();
var EventCreatedDate = new Date(e.created); // FamilyEvents[i].getDateCreated();
var EventStartDate = new Date(e.start.dateTime || e.start.date);// FamilyEvents[i].getStartTime();
var EventEndDate = new Date(e.end.dateTime || e.end.date); // FamilyEvents[i].getEndTime();
var EventCreator = e.creator.email; // FamilyEvents[i].getCreators(); //Gets the creator of the event to email notificaiton to
var EventID = e.id; // FamilyEvents[i].getID();
var CalendarID = calendarId; // FamilyCalendar.getId();
var eventUrl = e.htmlLink; // If you want to retrieve the hyperlink of the event, you can use this.
//Check if an event was created today AND does not have our names or "FYI" in its title
if (EventCreatedDate.valueOf() <= CurrentDate.valueOf() && EventTitle.indexOf('Name1') < 0 && EventTitle.indexOf('Name2') < 0 && EventTitle.indexOf('Name3') < 0 && EventTitle.indexOf('Name4') < 0 && EventTitle.indexOf('Name5') < 0 && EventTitle.indexOf('FYI') < 0) {
//Creates variables for the HTML body of the email notification. The same variables are referenced in the body of the HTML template.
var EmailMessage = HtmlService.createTemplateFromFile("EmailMessage"); //"EmailMessage" is the name of the HTML file in this script.
EmailMessage.recepient = EventCreator;
EmailMessage.eventTitle = EventTitle;
EmailMessage.eventStartDate = EventStartDate;
EmailMessage.eventEndDate = EventEndDate;
EmailMessage.calendarID = CalendarID;
EmailMessage.eventID = EventID;
// do something
};
});
}
In this sample script, eventUrl is the hyperlink of the event.
The Google Event ID can be determined if you have both the iCalUID and the corresponding CalendarID that event belongs to. And once you have the Event ID, assembling a URL for the event is a piece of cake.
Step 1
Grab the first part of the iCalUID.. up to but not including the # sign.
Step 2
Concatenate the string from step 1 with a the CalendarID separated by a single space.
Step 3
Use the built-in Utilities class to encode the string from step 2 to a web-safe base64 string.
Step 4
Assemble your url
let str = EventID.split('#')[0].toString();
let str2 = str + ' ' + CalendarID;
let eid = Utilities.base64EncodeWebSafe(str2, Utilities.Charset.UTF_8);
let url = 'https://www.google.com/calendar/event?eid=' + eid;
We can simplify all that into a one liner. Add it to your for loop after the CalendarID variable declaration.
let url = 'https://www.google.com/calendar/event?eid=' + Utilities.base64EncodeWebSafe(EventID.split('#')[0].toString() + ' ' + CalendarID, Utilities.Charset.UTF_8);
I have a column containing dates and in that some cell values are having values like " Due on Date". I have written a app script code that if a cell value contains "Due on" it will be copied to another column,else copied to different column.But on running I found that cells having "due on " on running the date and month are interchanged. for eg: if a cell contains "Due on 08/02/2022(dd/MM/yyyy)" is changed to "02/08/2022(MM/dd/yyyy)". Is there any method to retain the same date format.Here is the sample code:
function addYear(ss){
var value = ss.getDataRange().getValues();
value.shift();
for(var i=value.length-1;i>=0;i--){
var chn = value[i];
if(chn[2]!="NA"){
var rdate= new Date(chn[2]);
var dat=Utilities.formatDate(new Date(rdate), "GMT+5:30", "dd-MM-yyyy");
var mat= chn[2].toString();
if(mat.match(/Due on/)){
var d1= mat.toString().replace("Due on", "");
var ds = new Date(dat);
ss.getRange("C"+(i+2)).setValue("Valid till "+Utilities.formatDate(ds, "GMT+5:30", "dd-MMMM-yyyy"));
}
else{
var ds = new Date(chn[2]);
var yearadc = ds.getFullYear();
var monthadc = ds.getMonth();
var dayadc = ds.getDate();
var adc = new Date(yearadc + 1, monthadc, dayadc-1);
ss.getRange("C"+(i+2)).setValue("Valid till "+ Utilities.formatDate(adc, "GMT+5:30", "dd-MMMM-yyyy"));
}
}
I have made some changes to the code but if the day is greater than 12,the code reads as 01/01/1970. I am attaching a image relevant to this code.
I want to retain the original format as dd/MM/yyyy
Is there a way to make the .getEventById or .getEventSereiesById return anyhting other than null? I get valid ID for the initial event creation and can make that into a full functional URL but cannot use it in its native environment for its native purpose.
I am trying to make a basic google sheets schedule system that can refer to the calendar invite to check for changes and update the sheet or vise versa based on which side is further out in time. The system will be used in an environment where the scheduling has multiple users and meetings can be moved around a lot, generally further out in time. Everything works right up until i try to get information from the calendar even, .getStartTime(), due to the .getEvent calls returning null. not sure how to fix what other sources are telling me is a nonfunctional command that yet still "functions as intended".
function IEPscheduler() {
var spreadsheet = SpreadsheetApp.getActiveSheet(); // call sheet
//var calendarID = spreadsheet.getRange("H1").getValue();
var eventCal = CalendarApp.getCalendarById("mascharterschool.com_0edapns33khde9ig0di31i2mvc#group.calendar.google.com");
var signups = spreadsheet.getRange("A2:C2").getValues();
var lr = spreadsheet.getLastRow();
var lc = spreadsheet.getLastColumn(); //
var count = spreadsheet.getRange(2,1,lr-1,lc-1).getValues();// get meeting data
for (x=0; x<count.length; x++){
var shift = count[x]; // pull row from meeting data
var Start = shift[0];
var End = shift[1];
var Student = shift[2];
var guests = shift[3];
var description = shift[4];
var location = shift[5];
var run=shift[6]; // run following based on status column
// new meeting is scheduled
if(run == null || run == ''){
var event = {
'location': location,
'description':description ,
'guests':guests +',',
'sendInvites': 'True',
}
var invite = eventCal.createEvent(Student, Start, End, event);
invite.setGuestsCanInviteOthers(true); // allow guests to invite others
var eventId = invite.getId();
var date = invite.getDateCreated();
spreadsheet.getRange(x+2,7).setValue('Invite created'); // set status in sheet
spreadsheet.getRange(x+2,8).setValue(date); // inital date for created meeting invite
spreadsheet.getRange(x+2,9).setValue(eventId);
}
// check existing meetings for updates
else {
var id = shift[9];
var invite = eventCal.getEventSeriesById('id');
// if the time or location has changed update calander
if(invite.getStartTime() !== Start || invite.getEndTime() !== End || invite.getLocation() !== location){
// if sheet override flagged
if(shift[lc-1] !== null || Shift[lc-1] !== ''){
invite.setTime(Start,End); // update start/end time
invite.setLocation(location); // update location
}
// if canalder invite is further out than spreadsheet --> update spreadsheet
if(invite.getStartTime() >> Start){
spreadsheet.getRange(x+2,1).setValue();
spreadsheet.getRange(x+2,2).setValue();
}
// if spread sheet time is later than invite --> updater invite
else{
invite.setTime(Start,End); // update start/end time
invite.setLocation(location); // update location
}
var date = invite.getLastUpdate();
spreadsheet.getRange(x+2,7).setValue('Updated'); // set new status in sheet
spreadsheet.getRange(x+2,8).setValue(date); // set date meeting was updated
}
// if guest list has changed ???
if
}
}
}
// set script to be runnable from sheet tool bar
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Sync to Calendar') // tool bar banner
.addItem('Create Events Now', 'IEPscheduler') // sub catageory (title, function to run)
.addToUi();
}
We actually figured it out shortly after posting and I couldn't get back to this. Turns out the ID from .getId is the iCalUID and the .getEventById() takes a iCalID. The difference is that the UID has '#google.com' appended to the end of the ID. Split at the '#' and the get event works perfectly.
It's a stupid quirk that the getId command returns the right data in a useless form that requires editing to be used for its intended purpose.
No nulls returned for me with this script:
function getEvents() {
const cal=CalendarApp.getDefaultCalendar();
const dt=new Date();
const start=new Date(dt.getFullYear(),dt.getMonth()-1,dt.getDate());
const end=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate());
var events=cal.getEvents(start, end);
let eObj={idA:[],tA:[]}
events.forEach(function(ev,i){
eObj.idA.push(ev.getId());
eObj.tA.push(cal.getEventById(ev.getId()).getTitle());
});
Logger.log(JSON.stringify(eObj));
return eObj;
}
i am fairly new to coding in google scripts so the you will probably look at the code below and shake your head with its current inefficiency haha. in short this code is checking the value of cells A5 to D5 and seeing if its in the master list with the same values of E5 and F5. if its not it then copies A5-D5 to two different lists on another workbook. the code works at the moment but i was wondering if there is anyway to speed it up? its going to be mainly triggered by the IOS sheets app on an Ipad.(hence why i had to use onEdit instead of a button to trigger). when its checking to see if its already on the list, its on a local sheet in the same workbook that updates via a formula from the external workbook. Any help you could provide would be greatly appreciated. like i said though I'm new to coding i managed to piece this one together from the help of google when i ran into an issue.
function onEdit(e) {
var sheet1 = SpreadsheetApp.getActive().getSheetByName("sheet1")
var SiteName = sheet1.getRange("A5").getValue()
var Type = sheet1.getRange("B5").getValue()
var Fleet = sheet1.getRange("C5").getValue()
var Rego = sheet1.getRange("D5").getValue()
var Inside = sheet1.getRange("E5").getValue()
var Outside = sheet1.getRange("F5").getValue()
var User = sheet1.getRange("A3").getValue()
var master = SpreadsheetApp.getActive().getSheetByName("Master Wash List");
var True = true
var False = false
var mastermaster = SpreadsheetApp.openById("links to external workbook2").getSheetByName("master")
var mastertolocal = SpreadsheetApp.openById("links to external workbook2").getSheetByName("MasterToLocal")
var sheet = SpreadsheetApp.getActive().getSheetByName("Master Wash List");
var lastrow = sheet.getLastRow()
var SubmitButton = SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("G5").getValue()
if(Inside == True) {
for(var x = 1; x < lastrow+1;x++) {
var cell = master.getRange(x,3).getValue()
var masterinside = master.getRange(x, 4).getValue()
var masteruser = master.getRange(x,6).getValue()
if(Fleet == cell && masterinside != "N/A"){
SpreadsheetApp.getActive().getSheetByName("sheet1").insertRowAfter(6)
SpreadsheetApp.getActive().getSheetByName("sheet1").getRange(7,1).setValue([cell + " has already been Internally washed on " + masterinside + " by " + masteruser])
SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("G5").setValue("false")
}
}
}
if(Outside == True) {
for(var y = 2; y < lastrow+1;y++) {
var cell = master.getRange(y,3).getValue()
var masteroutside = master.getRange(y, 5).getValue()
var User1 = master.getRange(y, 6).getValue()
if(Fleet == cell && masteroutside != "N/A"){
SpreadsheetApp.getActive().getSheetByName("sheet1").insertRowAfter(6)
SpreadsheetApp.getActive().getSheetByName("sheet1").getRange(7,1).setValue([cell + " has already been externally washed on " + masteroutside + " by " + User1])
SpreadsheetApp.getActive().getSheetByName("sheet1").deleteRows(17,2)
SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("F5").setValue("false")}
}
}
if(SubmitButton == True){
if(Inside == True && Outside == True && Fleet != "" ) {
mastermaster.appendRow([SiteName, Type, Fleet, Rego, new Date(), new Date(), User,])
mastertolocal.appendRow([SiteName, Type, Fleet, Rego, "N/A", new Date(), User])
SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("E5").setValue("false")
SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("F5").setValue("false")
SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("G5").setValue("false")
SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("A5").setValue("")
SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("B5").setValue("")
SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("C5").setValue("")
SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("D5").setValue("")
SpreadsheetApp.getActive().getSheetByName("sheet1").insertRowAfter(6)
SpreadsheetApp.getActive().getSheetByName("sheet1").getRange(7,1).setValue([Fleet + " has successfully been Washed Internally & Externally."])
SpreadsheetApp.getActive().getSheetByName("sheet1").deleteRows(17, 2)}
else if(Outside == True && Fleet != "" ) {
mastermaster.appendRow([SiteName, Type, Fleet, Rego,"N/A", new Date(), User])
mastertolocal.appendRow([SiteName, Type, Fleet, Rego, "N/A", new Date(), User])
SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("F5").setValue("false")
SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("G5").setValue("false")
SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("A5").setValue("")
SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("B5").setValue("")
SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("C5").setValue("")
SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("D5").setValue("")
SpreadsheetApp.getActive().getSheetByName("sheet1").insertRowAfter(6)
SpreadsheetApp.getActive().getSheetByName("sheet1").getRange(7,1).setValue([Fleet + " has successfully been Washed Externally."])
SpreadsheetApp.getActive().getSheetByName("sheet1").deleteRows(17, 2) }
else if(Inside == True && Fleet != "") {
mastermaster.appendRow([SiteName, Type, Fleet, Rego, new Date(), "N/A", User])
mastertolocal.appendRow([SiteName, Type, Fleet, Rego, new Date(), "N/A", User])
SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("E5").setValue("false")
SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("G5").setValue("false")
SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("A5").setValue("")
SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("B5").setValue("")
SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("C5").setValue("")
SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("D5").setValue("")
SpreadsheetApp.getActive().getSheetByName("sheet1").insertRowAfter(6)
SpreadsheetApp.getActive().getSheetByName("sheet1").getRange(7,1).setValue([Fleet + " has successfully been Washed Internally."])
SpreadsheetApp.getActive().getSheetByName("sheet1").deleteRows(17, 2)}
else if(Inside != True && Outside != True && Fleet != "") {
SpreadsheetApp.getActive().getSheetByName("sheet1").insertRowAfter(6)
SpreadsheetApp.getActive().getSheetByName("sheet1").getRange(7,1).setValue(["Please select if " + Fleet + " Has been washed Internally, Externally or Both"])
SpreadsheetApp.getActive().getSheetByName("sheet1").deleteRows(17, 2)
SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("F5").setValue("false")}
}
}
Google has provided a Best Practices resource that you should definitely review. From there, the most relevant section is about using batch operations. Not specific to Google Apps Script, but a good practice, is to reduce some of the repetition in your code (see DRY principle). Here are some specific notes that you can apply to your entire script:
Reduce the number of times you call SpreadsheetApp.getActive() or SpreadsheeApp.openById() by placing the returned value into a variable.
var ss = SpreadsheetApp.getActive();
var externalSS = SpreadsheetApp.openById("links to external workbook2");
Reduce the number of times you call SpreadsheetApp.getActive().getSheetByName("sheet1") by using the variable that you already created sheet1. You can use find & replace-all to fix this.
Reduce the number of times you call getValue() by using getValues() instead and accessing the array values.
var row5 = sheet1.getRange(1, 1, 1, 7).getValues(); // A5:G5
var SiteName = row5[0][0]; // A5
var Type = row5[0][1]; // B5
var Fleet = row5[0][2]; // C5
var Rego = row5[0][3]; // D5
var Inside = row5[0][4]; // E5
var Outside = row5[0][5]; // F5
true and false are constants in javascript, so no need to place them into variables True & False.
Reduce the number of times you call new Date() by placing it in a variable;
var now = new Date();
Similary to point 3, reduce the number of times you call setValue() by instead using setValues().
row5.setValues([ // row5 previously defined A5:G5
[""], // A5
[""], // B5
[""], // C5
[""], // D5
[false], // E5
[false], // F5
[false] //G5
]);
The examples I wrote above are just examples, but they are based on your code. You'll need to go through your entire script and figure out exactly how to apply these principles without breaking the functionality of your script.