My onSheetOpen / showSidebar is not working for anyone but me when opening my events Google sheet. I have set up triggers to launch both the onSheetOpen and showSidebar for myself, but this is not working for anyone else who opens the spreadsheet. Can anyone help figure out why this might be the case? Below is the code I'm using.
/**
* Set up custom sidebar.
* TRIGGER: spreadsheet event 'on open'
*/
function onSheetOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Actions')
.addItem("Register interest in current opportunity", 'registerInterest')
.addItem("Show sidebar", 'showSidebar')
.addToUi();
showSidebar();
}
/**
* Renders the sidebar
* TRIGGER: spreadsheet event 'on open' & custom menu option
*/
function showSidebar() {
try {
var htmlOutput = HtmlService.createHtmlOutputFromFile('sidebar');
SpreadsheetApp.getUi().showSidebar(htmlOutput);
} catch(e) {
// Just ignore - means user doesn't have edit access to sheet
}
}
/**
* Main function. Grabs the required data from the currently-selected row, registers interest
* in that data in a separate sheet, and sends a confirmation email to the user.
*/
function registerInterest() {
//var spreadsheet = SpreadsheetApp.getActive();
// These are the attributes we capture and store in the 'interest' sheet
var who = Session.getActiveUser().getEmail();
var when = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'yyyy-MM-dd HH:mm:ss');
var data = getRowOfInterest(SpreadsheetApp.getActiveSheet());
var id = Utilities.getUuid();
Logger.log(id)
if(data.what && data.what.length > 0) {
// Get the Google Sheets ID of the target 'interest' sheet
var id = getConfigValue("InterestSheet");
if(id) {
// Now try and open that sheet for updating
var ss = SpreadsheetApp.openById(id);
if(ss) {
var sheet = ss.getSheetByName('Interest');
if(sheet) {
// All good; log interest. First, create the new row of data for the target sheet
sheet.appendRow([ when, who, data.what, data.subject, data.location, data.industry, data.capabilities ]);
// Second, grab our 'template' email from config & use that to send a confirmation
// email to the person registering their interest
var body = getConfigValue('InterestEmail');
MailApp.sendEmail(who, getConfigValue('InterestSubject'), null, { noReply: true, htmlBody: body });
} else {
throw Error('can\'t open the \'Interest expressed\' sheet (or it doesn\'t exist)');
}
} else {
throw Error('can\'t open the \'Interest expressed\' sheet');
}
} else {
throw Error('\'Interest expressed\' sheet not specified');
}
}
}
/**
* Utility to derive the required data for registering interest.
* Called from the registerInterest() function
*/
function getRowOfInterest(sheet) {
var result = {};
var row = sheet.getActiveRange().getRow(); // Get the currently-selected row index
var fullRange = sheet.getRange(row, 1, 1, sheet.getLastColumn()); // Get the entire row of data
//var fullRange = sheet.getRange(row, 1, 1, 9);
Logger.log(fullRange.getValues())
var data = fullRange.getValues();
if(data[0] && data[0].length > 0) {
for(var n = 0; n < data[0].length; n++) {
// Populate specific attributes in the 'result' object - tailor this as you see fit
if(n==0) result.subject = data[0][n];
if(n==6) result.what = data[0][n] + ' ';
//if(n==5 || n==7) result.what += data[0][n] + ' ';
if(n==7) result.location = data[0][n];
if(n==11) result.industry = data[0][n];
if(n==12) result.capabilities = data[0][n];
}
result.what += '(' + sheet.getName() + ')';
result.industry;
result.capabilities;
return result;
}
}
function getSomeRange(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var row = sheet.getActiveRange().getRow(); // Get the currently-selected row index
var myRange = sheet.getRange(row, 1, 1, sheet.getLastColumn());
//Logger.log("Number of rows in range is "+myRange.getNumRows()+ " Number of columns in range is "+ myRange.getNumColumns());
}
/**
* Utility to pull specified data from a config sheet. This assumes that a sheet called 'Config'
* is populated thus:
* Column contains a load of 'key' and column 2 contains the corresponding 'values'
* Called from registerInterest() function
*/
function getConfigValue(key) {
var sheet = SpreadsheetApp.getActive().getSheetByName('Config');
var result;
if(sheet) {
// Scan column 1 for key and return value in column 2
var data = sheet.getDataRange().getValues();
for(var n = 0; n < data.length; ++n) {
if(data[n][0].toString().match(key)) result = data[n][1];
}
}
return result;
}
function hideExpiredEvents() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Events');
var exsh=ss.getSheetByName('Expired Events');
var sr=2;
var rg=sh.getRange(sr,1,sh.getLastRow()-sr+1,sh.getLastColumn());
var vA=rg.getValues();
Logger.log(vA);
var dt=new Date();
var d=0;//deleted row counter
var today=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate(0)).valueOf();
for(var i=0;i<vA.length;i++) {
if(new Date(vA[i][3]).valueOf()<today) {
exsh.appendRow(vA[i]);//move to bottom of expired events
sh.deleteRow(i+sr-d++);//increment deleted row counter
}
}
}
Unfortunately, it seems that the issue you are encountering might be a bug.
What you can do in this case is to star the issue on Issue Tracker here by clicking the ★ next to the issue number and post a comment as well saying that you are affected by the issue.
Related
I'm having issues trying to deal with the "Exceeded maximum execution time" error I get when running my script in Google sheets. I've found a few solutions on here that I couldn't get working with my script. Any help would be greatly appreciated, here is the script I am trying to modify:
function getGeocodingRegion() {
return PropertiesService.getDocumentProperties().getProperty('GEOCODING_REGION') || 'au';
}
function addressToPosition() {
// Select a cell with an address and two blank spaces after it
var sheet = SpreadsheetApp.getActiveSheet();
var cells = sheet.getActiveRange();
var addressColumn = 1;
var addressRow;
var latColumn = addressColumn + 1;
var lngColumn = addressColumn + 2;
var API_KEY = "xxx";
var options = {
muteHttpExceptions: true,
contentType: "application/json",
};
for (addressRow = 1; addressRow <= cells.getNumRows(); ++addressRow) {
var address = cells.getCell(addressRow, addressColumn).getValue();
var serviceUrl = "https://maps.googleapis.com/maps/api/geocode/json?address=" + address + "&key=" + API_KEY;
// Logger.log(address);
// Logger.log(serviceUrl);
var response = UrlFetchApp.fetch(serviceUrl, options);
if (response.getResponseCode() == 200) {
var location = JSON.parse(response.getContentText());
// Logger.log(response.getContentText());
if (location["status"] == "OK") {
//return coordinates;
var lat = location["results"][0]["geometry"]["location"]["lat"];
var lng = location["results"][0]["geometry"]["location"]["lng"];
cells.getCell(addressRow, latColumn).setValue(lat);
cells.getCell(addressRow, lngColumn).setValue(lng);
}
}
}
};
function positionToAddress() {
var sheet = SpreadsheetApp.getActiveSheet();
var cells = sheet.getActiveRange();
// Must have selected 3 columns (Address, Lat, Lng).
// Must have selected at least 1 row.
if (cells.getNumColumns() != 3) {
Logger.log("Must select at least 3 columns: Address, Lat, Lng columns.");
return;
}
var addressColumn = 1;
var addressRow;
var latColumn = addressColumn + 1;
var lngColumn = addressColumn + 2;
//Maps.setAuthentication("acqa-test1", "AIzaSyBzNCaW2AQCCfpfJzkYZiQR8NHbHnRGDRg");
var geocoder = Maps.newGeocoder().setRegion(getGeocodingRegion());
var location;
for (addressRow = 1; addressRow <= cells.getNumRows(); ++addressRow) {
var lat = cells.getCell(addressRow, latColumn).getValue();
var lng = cells.getCell(addressRow, lngColumn).getValue();
// Geocode the lat, lng pair to an address.
location = geocoder.reverseGeocode(lat, lng);
// Only change cells if geocoder seems to have gotten a
// valid response.
Logger.log(location.status);
if (location.status == 'OK') {
var address = location["results"][0]["formatted_address"];
cells.getCell(addressRow, addressColumn).setValue(address);
}
}
};
function generateMenu() {
var entries = [{
name: "Geocode Selected Cells (Address to Lat, Long)",
functionName: "addressToPosition"
}, {
name: "Geocode Selected Cells (Address from Lat, Long)",
functionName: "positionToAddress"
}];
return entries;
}
function updateMenu() {
SpreadsheetApp.getActiveSpreadsheet().updateMenu('Geocode', generateMenu())
};
/**
* Adds a custom menu to the active spreadsheet, containing a single menu item
* for invoking the readRows() function specified above.
* The onOpen() function, when defined, is automatically invoked whenever the
* spreadsheet is opened.
*
* For more information on using the Spreadsheet API, see
* https://developers.google.com/apps-script/service_spreadsheet
*/
function onOpen() {
SpreadsheetApp.getActiveSpreadsheet().addMenu('Geocode', generateMenu());
};
Or, any other script you may know of that does geocode in Google sheets and already properly handles max execution time that would be OK too, I'm not tied to this specific script, just getting the outcome I need!
This error's cause is due to script running more than 6 minutes.
A possible solution is to limit the time-consuming part of your script (which is the for loop) to only 5 minutes. Then create a trigger and continue the loop into another instance if it still isn't done.
Script:
function addressToPosition() {
// Select a cell with an address and two blank spaces after it
var sheet = SpreadsheetApp.getActiveSheet();
...
var options = {
muteHttpExceptions: true,
contentType: "application/json",
};
// if lastRow is set, get value, else 0
var continueRow = ScriptProperties.getProperty("lastRow") || 0;
var startTime = Date.now();
var resume = true;
for (addressRow = ++continueRow; addressRow <= cells.getNumRows(); ++addressRow) {
var address = cells.getCell(addressRow, addressColumn).getValue();
...
// if 5 minutes is done
if ((Date.now() - startTime) >= 300000) {
// save what's the last row you processed then exit loop
ScriptProperties.setProperty("lastRow", addressRow)
break;
}
// if you reached last row, assign flag as false to prevent triggering the next run
else if (addressRow == cells.getNumRows())
resume = false;
}
// if addressRow is less than getNumRows()
if (resume) {
// after execution of loop, prepare the trigger for the same function
var next = ScriptApp.newTrigger("addressToPosition").timeBased();
// run script after 1 second to continue where you left off (on another instance)
next.after(1000).create();
}
}
Do the same thing with your other functions.
I'm looking to create a spreadsheet where it logs data and the changes you make on it.
For example in sheet, I used a Google Script to do it but instead of only logging in a particular row when you change the data on Sheet1, it copies all the data you changed since the beginning on Sheet 2. I only want to log a particular row each time I make a change.
Here's my code
* Retrieves all the rows in the active spreadsheet that contain Yes
* in the Include column and copies them to the Report sheet.
*/
function myFunction() {
var sSheet = SpreadsheetApp.getActiveSpreadsheet();
var srcSheet = sSheet.getSheetByName("Sheet1");
var tarSheet = sSheet.getSheetByName("Sheet2");
var lastRow = srcSheet.getLastRow();
for (var i = 2; i <= lastRow; i++) {
var cell = srcSheet.getRange("B" + i);
var val = cell.getValue();
if (val == 'Yes') {
var srcRange = srcSheet.getRange("A" + i + ":D" + i);
var tarRow = tarSheet.getLastRow();
tarSheet.insertRowAfter(tarRow);
var tarRange = tarSheet.getRange("A" + (tarRow+1) + ":D" + (tarRow+1));
srcRange.copyTo(tarRange);
}
}
};
Can anyone please help?
Use onEdit(e)
function onEdit(e) {
var srcSheet = e.source.getActiveSheet();
if (srcSheet.getSheetName() !== 'Sheet1') { return; }
var row = e.range.rowStart;
var cols = 4;
var srcRange = srcSheet.getRange(row, 1, 1, cols);
var values = srcRange.getValues()[0];
if (values[1] == 'Yes') {
var tarSheet = e.source.getSheetByName('Sheet2');
tarSheet.appendRow(values);
}
}
So I have looked through many questions regarding this issue and tried all the fixes and it still does not run my code.
I have tested it so that it doesn't create the form and a method where it does create the form in the spreadsheet and both fail to update the cell I want it to update.
To do the other method I simply removed the top 3 functions from the code and created a new trigger that calls when the form is submitted.
I have tested if the function has the ability to update the correct cell and it does so properly. However, it for some reason can't do it when I introduce the form data elements.
If anyone could help me through this issue I would greatly appreciate it!
/**
* A special function that inserts a custom menu when the spreadsheet opens.
*/
function onOpen() {
var menu = [{name: 'Create RSVP', functionName: 'RSVP_'}];
SpreadsheetApp.getActive().addMenu('RSVP', menu);
}
/**
* A set-up function
*/
function RSVP_() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName('Sheet1');
setUpForm_(ss);
ScriptApp.newTrigger('onFormSubmit').forSpreadsheet(ss).onFormSubmit().create();
ss.removeMenu('RSVP');
}
/**
* Creates a Google Form that allows students to RSVP
*
*/
function setUpForm_(ss) {
var form = FormApp.create('RSVP Form');
form.setDestination(FormApp.DestinationType.SPREADSHEET, ss.getId());
form.addTextItem().setTitle('First Name').setRequired(true);
form.addTextItem().setTitle('Last Name').setRequired(true);
form.addTextItem().setTitle('Email').setRequired(true);
form.addMultipleChoiceItem().setTitle('Are you still interested in attending?')
.setChoiceValues(['Yes','No']).setRequired(true);
}
/**
* Updates RSVP cell
*
* #param {Object} e The event parameter for form submission to a spreadsheet;
* see https://developers.google.com/apps-script/understanding_events
*/
function onFormSubmit(e) {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName('Sheet1');
var range = sheet.getDataRange();
var values = range.getValues();
for (var i = 1; i < values.length; i++) {
var session = values[i];
var title = session[2];
if (e.namedValues['Email'][0] && e.namedValues['Email'][0] == title) {
sheet.getRange(i+1, 5, 1, 1).setValue([[e.namedValues['Are you still interested in attending?'][0]]])
}
}
var sheet = ss.getSheetByName('Sheet2');
var nextRow = sheet.getLastRow()+1;
sheet.getRange(nextRow, 1, 1, 3).setValues([[e.namedValues['First Name'][0], e.namedValues['Last Name'][0], e.namedValues['Email'][0], e.namedValues['Are you still interested in attending?'][0]]]);
}
try this:
function onFormSubmit(e) {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName('Sheet1');
var range = sheet.getDataRange();
var values = range.getValues();
for (var i = 1; i < values.length; i++) {
if (e.namedValues['Email'] == values[i][2]) {
sheet.getRange(i+1, 5, 1, 1).setValue(e.namedValues['Are you still interested in attending?'])
}
}
}
I've been searching for the answer to this question but have so far been unable to piece together the answer. Please explain any answer you have in really simple terms as I'm fairly new to GAS and RegEx. I've got most of the syntax down but the execution of it in GAS is giving me a hard time.
Basically, I want to write a script that, when the spreadsheet is edited, checks which rows have yet to be merged. Then, on those rows, creates a copy of a template Google Doc and names the document based on the spreadsheet data. From there (this is the hard part), I need it to replace merge tags in the template with the data from the spreadsheet.
The tags in the templates I'll be using look like this: <<mergeTag>>
My idea was to match the whole tag, and replace it with data from the spreadsheet that exists in the column with the same name as what's inside the "<<>>". Ex: <<FooBar>> would be replaced with the data from the column named FooBar. It would obviously be from the current row that needs the merging.
After that, all that's left is to send an email (a few more row-specific personalization) with that document attached (sometimes as a PDF) with the body of the message coming from an HTML file elsewhere in the project.
This is the whole thing I have so far (notice the placeholders here and there that I can personalize for each spreadsheet I use this for):
function onEdit() {
//SPREADSHEET GLOBAL VARIABLES
var ss = SpreadsheetApp.getActiveSpreadsheet();
//get only the merge sheet
var sheet = ss.getSheetByName("Merge Data");
//get all values for later reference
var range = sheet.getActiveRange();
var values = range.getValues();
var lastRow = range.getLastRow();
var lastColumn = range.getLastColumn();
//get merge checker ranges
var urlColumn = range.getLastColumn();
var checkColumn = (urlColumn - 1);
var checkRow = range.getLastRow();
var checkRange = sheet.getRange(2, checkColumn, checkRow);
var check = checkRange.getBackgrounds();
//get template determination range (unique to each project)
var tempConditionRange = sheet.getRange(row, column);
var tempConditionCheck = tempConditionRange.getValues();
//set color variables for status cell
var red = "#FF0000";
var yellow = "#FFCC00";
var green = "#33CC33";
//////////////////////////////////////////////////////////
//DOC GLOBAL VARIABLES
var docTemplate1 = DriveApp.getFileById(id);
var docTemplate2 = DriveApp.getFileById(id);
var docTemplate3 = DriveApp.getFileById(id);
var folderDestination = DriveApp.getFolderById(id);
//////////////////////////////////////////////////////////
//EMAIL GLOBAL VARIABLES
var emailTag = ss.getRangeByName("Merge Data!EmailTag");
var personalizers = "";
var subject = "" + personalizers;
var emailBody = HtmlService.createHtmlOutputFromFile("Email Template");
//////////////////////////////////////////////////////////
// MERGE CODE
for (i = 0; i < check.length; i++) {
//for rows with data, check if they have already been merged
if (check[i] == green) {
continue;
} else {
var statusCell = sheet.getRange((i+2), checkColumn, 1, 1);
var urlCell = sheet.getRange((i+2), urlColumn, 1, 1);
var dataRow = sheet.getRange((i+2), 1, lastRow, (lastColumn - 2))
statusCell.setBackground(red);
//for rows with data, but not yet merged, perform the merge code
//////////////////////////////////////////////////////////
//DOC CREATION
//Determine which template to use
if (tempConditionCheck[i] == "") {
var docToUse = docTemplate1;
}
if (tempConditionCheck[i] == "") {
var docToUse = docTemplate2;
}
if (tempConditionCheck[i] == "") {
var docToUse = docTemplate3;
}
//Create a copy of the template
//Rename the document using data from specific columns, at specific rows
//Move the doc to the correct folder
var docName = "";
var docCopy = docToUse.makeCopy(docName, folderDestination);
var docId = docCopy.getId();
var docURL = docCopy.getUrl();
var docToSend = DriveApp.getFileById(docId);
var docBody = DocumentApp.openById(docId).getBody();
Here's where I need the help
//Locate the Merge Tags
//Match Merge Tags to the column headers of the same name
//Replace the Merge Tags with the data from the matched column, from the correct row
function tagReplace() {
var tagMatch = "/(<{2}(\w+)>{2})/g";
}
statusCell.setBackground(yellow);
urlCell.setValue(docURL);
The rest is just finishing up the process
//////////////////////////////////////////////////////////
//EMAIL CREATION
//Create an email using an HTML template
//Use Merge Tags to personalize email
//Attach the doc we created to the email
//Send email to recipients based on data in the sheet
MailApp.sendEmail(emailTag, subject, emailBody, {
name: "Person McPerson",
attachments: [docToSend], //[docToSend.getAs(MIME.PDF)],
html: emailBody,
});
//////////////////////////////////////////////////////////
//CHECK ROW UPDATE
statusCell.setBackground(green);
}
}
}
My sheets all have a frozen first row that acts as the header row. All my columns will be consistently named the exact same thing as the tags (minus the <<>>).
How do I match the tags to the data?
EDIT
```````````````````
The solution did not work as described when I inserted it into my code as follows:
function formMerge() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Merge Data");
var urlColumn = sheet.getMaxColumns();
var checkColumn = urlColumn - 1;
var lastRow = ss.getSheetByName("Form Responses").getLastRow();
var values = sheet.getDataRange().getValues();
var headers = values[0];
var urlRange = sheet.getRange(2, urlColumn, lastRow);
var checkRange = sheet.getRange(2, checkColumn, lastRow);
var check = checkRange.getBackgrounds();
var red = "#ff0404";
var yellow = "#ffec0a";
var green = "#3bec3b";
var docTemplate = DriveApp.getFileById(id);
var folderDestination = DriveApp.getFolderById(id);
// MERGE CODE
for (i = 0; i < check.length; i++) {
if (check[i] == green) {
continue;
} else {
var statusCell = sheet.getRange((i+2), checkColumn, 1, 1);
var urlCell = sheet.getRange((i+2), urlColumn, 1, 1);
var dataRow = sheet.getRange((i+2), 1, 1, (urlColumn - 2)).getValues();
var clientNameRange = sheet.getRange((i+2), 3);
var clientName = clientNameRange.getValue();
var dateRange = sheet.getRange((i+2), 2);
var datePreFormat = dateRange.getValue();
var timeZone = CalendarApp.getTimeZone();
var date = Utilities.formatDate(new Date(datePreFormat), timeZone, "MM/dd/yyyy");
statusCell.setBackground(red);
//EMAIL VARIABLES
var personalizers = clientName;
var subject = "Post Intake Report for " + personalizers;
var emailBody = "Please see the attached Google Doc for the Post Intake Report for " + clientName + ". The intake was performed on " + date + ".";
var emailTagRange = sheet.getRange((i+2), 24);
var emailTagValue = emailTagRange.getValue();
var emailTag = emailTagValue.split(", ");
//DOC CREATION
var docToUse = docTemplate;
var docName = "Post Intake Report - " + clientName + " [" + date + "]";
var docCopy = docToUse.makeCopy(docName, folderDestination);
var docId = docCopy.getId();
var docURL = docCopy.getUrl();
var docBody = DocumentApp.openById(docId).getBody().editAsText();
for (var j=0; j<headers.length; j++) {
var re = new RegExp("(<<"+headers[j]+">>)","g");
docBody.replaceText(re, dataRow[j]);
}
statusCell.setBackground(yellow);
urlCell.setValue(docURL);
//EMAIL CREATION
MailApp.sendEmail(emailTag, subject, emailBody, {
name: "Christopher Anderson",
attachments: [docCopy],
html: emailBody
});
statusCell.setBackground(green);
}
}
}
Build the RegExp for each tag on the fly, using the header values from your spreadsheet.
Use Body.replaceText() to perform the replacements.
var values = sheet.getDataRange().getValues();
var headers = values[0];
...
// Loop over all columns. Use header names to search for tags.
for (var col=0; col<headers.length; col++) {
// Build RegExp using column header
var re = new RegExp("(<{2}"+headers[col]+">{2})","g");
// Replace tags with data from this column in dataRow
body.replaceText(re, dataRow[col]);
}
This snippet will operate on a single row; the first couple of declarations should appear outside of your row loop. The column looping is then done after you've created and opened the new document, and obtained the body object.
It loops over all the columns in the spreadsheet, using the header names to find the tags you've defined, and replaces them with the corresponding cell contents for the current row.
I have a spreadsheet with revenue and expense data. Here is a spreadsheet with what it looks like with data redacted: https://docs.google.com/spreadsheets/d/1xYXt_pvhpzCO7D61SoOwcmNNIGaFHkExwLHhl56c-NU/edit?usp=sharing
I want to create a script that validates the data in column C by forcing a user to enter a date. Once a date is entered, I want to return a new row beneath it. The code works in that it validates data and enters a new row, but when I enter a date into C96, the new row pushes the 'Date' cell, currently in C100, down to C101 and says the date is invalid. I set up my code so that it should be rerunning the script and not making C101 data validated. Additionally, I am getting errors with the 'getRange' method. It is saying that 'TypeError: Cannot call method "getRange" of undefined.'
Currently I have triggers for onEdit and onOpen for both dateValidation functions and onEdit triggers for the addRow functions.
For some reason, on my original file, the code still runs despite these errors. I have starred the location of the errors in my code below. I am very new to Google Scripts/ programming in general, so if anyone can help me out it would be greatly appreciated.
Here is my code currently:
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("General Ledger");
// Get row of first revenue
var firstRowRevLookup = 'Revenue';
var firstRowRange = sheet.getRange('B1:B');
var firstRowValues = firstRowRange.getValues();
var x = [];
for (var i = 0; i<firstRowValues.length; i++) {
if(firstRowValues[i] == firstRowRevLookup) {
x.push(i+3);
}
}
var firstRowRevenue = Number(x);
Logger.log(firstRowRevenue);
//Get row of last revenue
var lastRowRevLookup = 'End Revenues';
var lastRowRange = sheet.getRange('C1:C');
var lastRowValues = lastRowRange.getValues();
var y = [];
for (var i = 0; i < lastRowValues.length; i++) {
if(lastRowValues[i] == lastRowRevLookup) {
y.push(i);
}
}
var lastRowRevenue = Number(y);
Logger.log(lastRowRevenue)
//Get row of first expense
var firstRowExLookup = 'Expenses';
var z = [];
for(var i = 0; i<firstRowValues.length; i++) {
if(firstRowValues[i] == firstRowExLookup){
z.push(i+3);
}
}
var firstRowExpense = Number(z);
Logger.log(firstRowExpense);
//Get row of last expense
var lastRowExLookup = 'End Expenses';
var q = [];
for(var i = 0; i <lastRowValues.length; i++) {
if( lastRowValues[i] == lastRowExLookup) {
q.push(i);
}
}
var lastRowExpense = Number(q);
Logger.log(lastRowExpense);
dateValidationExpenses(sheet, firstRowExpense, lastRowExpense);
dateValidationRevenue(sheet, firstRowRevenue, lastRowRevenue);
//User must enter date into column C
function dateValidationExpenses(sheet, firstRowExpense, lastRowExpense) {
// Logger.log(sheet);
**var dateRange = sheet.getRange(firstRowExpense, 3, lastRowExpense-firstRowExpense+1);**
//Logger.log(dateRange);
var rule = SpreadsheetApp.newDataValidation()
.requireDate()
.setAllowInvalid(false)
.setHelpText('Please enter date using format "=date(year,month,day)"')
.build();
dateRange.setDataValidation(rule);
dateRange.setFontWeight('normal');
}
//User must enter date into column C
function dateValidationRevenue(sheet,firstRowRevenue,lastRowRevenue) {
**var dateRange = sheet.getRange(firstRowRevenue, 3, lastRowRevenue-firstRowRevenue+1);**
var rule = SpreadsheetApp.newDataValidation()
.requireDate()
.setAllowInvalid(false)
.setHelpText('Please enter a date using format "=date(year,month,day)"')
.build();
dateRange.setDataValidation(rule);
dateRange.setFontWeight('normal');
}
function addRowExpenses () {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('General Ledger');
var testCell = sheet.getRange(lastRowExpense,3).getValue();
//Logger.log(testCell);
if (testCell !== '') {
sheet.insertRows(lastRowExpense+1);
}
}
function addRowRevenue () {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('General Ledger');
var testCell = sheet.getRange(lastRowRevenue,3).getValue();
//Logger.log(testCell);
if (testCell !== '') {
sheet.insertRows(lastRowRevenue+1);
}
}
function editTrigger1() {
var sheet = SpreadsheetApp.getActive()
ScriptApp.newTrigger('addRowExpenses')
.forSpreadsheet(sheet)
.onEdit()
.create();
}
function editTrigger2() {
var sheet = SpreadsheetApp.getActive()
ScriptApp.newTrigger('addRowRevenue')
.forSpreadsheet(sheet)
.onEdit()
.create();
}
The variable sheet is declared outside of a function, which makes it a "global". You don't need to pass sheet
Currently:
dateValidationExpenses(sheet, firstRowExpense, lastRowExpense);
dateValidationRevenue(sheet, firstRowRevenue, lastRowRevenue);
Change to:
dateValidationExpenses(firstRowExpense, lastRowExpense);
dateValidationRevenue(firstRowRevenue, lastRowRevenue);
sheet is available to the dateValidationExpenses function without the need to pass it. That should solve the issue with the error you are getting on this line:
var dateRange = sheet.getRange(firstRowExpense, 3, lastRowExpense-firstRowExpense+1);