I made a spreadsheet where I can easily manage my events in my google Calendar.
In the spreadsheet Row 1 has the dates, Row 2 has the specifics of the appointment and Row 3 saves the eventId of the event created by the script.
Every week starts on a new row.
The script works like it should and looks like this:
function CalenderUpdate() {
var calId = "xxxxxxxxxxxxxxxx#group.calendar.google.com";
var descr = "";
var date
var titel
var sheet = SpreadsheetApp.getActiveSheet();
var Afspraak = sheet.getRange(2,1,3,5).getValues();
var cal = CalendarApp.getCalendarById(calId);
for (j=2;j<9;j=j+3){
Afspraak = sheet.getRange(j,1,3,5).getValues();
for (i=0; i<5; i++){
descr ="";
date = Afspraak[0][i];
titel = Afspraak[1][i];
if (titel == "A"){ descr = "Late 14:00 - 21:00";}
if (titel == "M"){ descr = "Early 6:00 - 14:00";}
if (Afspraak[1][i] != ""){
var event = cal.createAllDayEvent(titel,date,{description:descr});
sheet.getRange(j+2,i+1).setValue(event.getId());
}
}
}
}
Now I want a new function. If I edit something in the schedule, I want it to update in my calendar. So I added a function onEdit that would delete the event if one created on that day, and if necessary create a new.
I started with this code to delete the event if something is edited. But it doesn't delete the appointment and I can't figure out why.
function onEdit(e){
var ss = SpreadsheetApp.getActiveSheet();
var range = e.range;
var rij = range.getRow();
var col = range.getColumn();
var afspraakId = ss.getRange(rij+1,col).getValue();
if (rij % 3 == 0) { ss.getRange(1,1).setValue(afspraakId); }
var calId = "xxxxxxxxxxxxxxx#group.calendar.google.com";
var cal = CalendarApp.getCalendarById(calId);
var event = cal.getEventSeriesById(id);
event.deleteEventSeries();
}
Hope someone can help me?
EDIT:
I changed the onEdit(e) to onEditInstallable(e) but the script never get triggered.
Even if I add a trigger by the menu
What do I do wrong?
EDIT2:
I've done some editing on my script. This is the end result:
function test_onEdit() {
onEditInstallable({
user : Session.getActiveUser().getEmail(),
source : SpreadsheetApp.getActiveSpreadsheet(),
range : SpreadsheetApp.getActiveSpreadsheet().getActiveCell(),
value : SpreadsheetApp.getActiveSpreadsheet().getActiveCell().getValue(),
authMode : "LIMITED"
});
}
function onEditInstallable(e){
var ss = SpreadsheetApp.getActiveSheet();
var range = e.range;
var rij = range.getRow();
var col = range.getColumn();
if (rij % 3 == 0 && col < 8) {
var cal = CalendarApp.getCalendarById("xxxxxxxxxxxxxxxxxx#group.calendar.google.com");
var event
var geg = ["","",""];
var descr ="";
for (i = 0 ; i < 3 ; i++){ geg[i] = ss.getRange(rij-1+i,col).getValue();}
if (geg[2] != ""){
event = cal.getEventSeriesById(geg[2]);
event.deleteEventSeries();
ss.getRange(rij+1,col).setValue("");
}
if (geg[1] != ""){
event = cal.createAllDayEvent(geg[1],geg[0],{description:descr});
ss.getRange(rij+1,col).setValue(event.getId());
}
}
}
The script works if I run the test_OnEdit. But the onEditInstallable doesn't trigger automatic? I'm an amateur in programming and I don't understand much of this page. Hope someone can help me figure this out.
Your second edit is a good approach, I'd suggest to add a couple of logs in the script to check the condition values.
I'd rather use the comments for this but it would definitely be too long and hard to read so I use the 'answer' format.
function onEditInstallable(e){
var ss = SpreadsheetApp.getActiveSheet();
var range = e.range;
var rij = range.getRow();
var col = range.getColumn();
Logger.log('rij = '+rij+' col = '+col);
if (rij % 3 == 0 && col < 8) {
var cal = CalendarApp.getCalendarById("xxxxxxxxxxxxxxxxxx#group.calendar.google.com");
var event
var geg = ["","",""];
var descr ="";
for (i = 0 ; i < 3 ; i++){ geg[i] = ss.getRange(rij-1+i,col).getValue();}
Logger.log('geg[2] = '+geg[2]);
if (geg[2] != ""){
event = cal.getEventSeriesById(geg[2]);
event.deleteEventSeries();
ss.getRange(rij+1,col).setValue("");
}
if (geg[1] != ""){
event = cal.createAllDayEvent(geg[1],geg[0],{description:descr});
ss.getRange(rij+1,col).setValue(event.getId());
}
}
}
I used this last piece of code and linked the test_onEdit script to a button in the spreadsheet. On the first time testing it from there Google gave a pop-up asking for extra authorizations. From then on everything works like a charm.
Now my spreadsheet automatically edits my agenda if I change something in the spreadsheet.
Thanks for the help everyone.
function test_onEdit() {
onEditInstallable({
user : Session.getActiveUser().getEmail(),
source : SpreadsheetApp.getActiveSpreadsheet(),
range : SpreadsheetApp.getActiveSpreadsheet().getActiveCell(),
value : SpreadsheetApp.getActiveSpreadsheet().getActiveCell().getValue(),
authMode : "LIMITED"
});
}
function onEditInstallable(e){
var ss = SpreadsheetApp.getActiveSheet();
var range = e.range;
var rij = range.getRow();
var col = range.getColumn();
Logger.log('rij = '+rij+' col = '+col);
if (rij % 3 == 0 && col < 8) {
var cal = CalendarApp.getCalendarById("xxxxxxxxxxxxxxxxxx#group.calendar.google.com");
var event
var geg = ["","",""];
var descr ="";
for (i = 0 ; i < 3 ; i++){ geg[i] = ss.getRange(rij-1+i,col).getValue();}
Logger.log('geg[2] = '+geg[2]);
if (geg[2] != ""){
event = cal.getEventSeriesById(geg[2]);
event.deleteEventSeries();
ss.getRange(rij+1,col).setValue("");
}
if (geg[1] != ""){
event = cal.createAllDayEvent(geg[1],geg[0],{description:descr});
ss.getRange(rij+1,col).setValue(event.getId());
}
}
}
Try this:
function deleteEvent(event) {
if (typeof event != 'undefined') {
Logger.log("Deleting event %s", event.getTitle())
event.deleteEvent()
}
}
function deleteEvents(eventCal,startTime,endTime,title){
// var oldEvents = eventCal.getEvents(startTime, endTime, {search: title});
var oldEvents = eventCal.getEvents(startTime, endTime);
Logger.log("oldEvents %s", oldEvents);
for (var j = 0; j < oldEvents.length; j++){
Logger.log("oldEvents[j] %s", oldEvents[j]);
deleteEvent(oldEvents[j]);
}
}
Related
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()
I'm trying to have TIMEVALUE set off a script. I have column P which is a Timer column that is subtracting a time from NOW to get a time value and then I am converting that to a TIMEVALUE in column O. When that value is either below or above certain values I want column N to then have a value so it would trigger my checkboxes script. But for some reason I can't get the TIMEVALUE to trigger. I tried to put it in another column and have the values CopyAndPasted Values Only into a column with that run on a time trigger of every minute, but apparently that doesn't count as an Edit or it's not reading the DisplayValue.
function onEdit(e) {
timeValue(e);
checkboxes(e);
rangerTime(e);
}
function rangerTime(e){
var editRow = e.range.getRow();
var editColumn = e.range.getColumn();
if (editColumn === 13 && e.value == "TRUE") {
const sh = e.range.getSheet();
sh.getRange(editRow, 25).setValue(sh.getRange(editRow, 25).getValue()+1);
sh.getRange(editRow, 13).setValue(" ")
}
}
function timeValue(e) {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getActiveSheet();
var editRow = e.range.getRow();
var editColumn = e.range.getColumn();
if(editRow > 3) {
var rowRange = sheet.getRange("O" + editRow);
var kCell = sheet.getRange("K" + editRow);
var kValue = kCell.getValue();
var nCell = sheet.getRange("N" + editRow);
var kHasValue = kValue != "";
if(editColumn > 10) {
if(rowRange.getDisplayValue()<0.02 && !kHasValue){
nCell.setValue(1);
}
if (rowRange.getDisplayValue()>0.5 && !kHasValue){
nCell.setValue(1);
}
if(kHasValue) {
nCell.setValue(" ");
}
}
}
}
function checkboxes(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ui = SpreadsheetApp.getUi();
var names = ss.getRange("N4:N");
var namesValues = names.getValues();
var checkboxes = ss.getRange("M4:M");
var cbRows = checkboxes.getHeight();
var cbValues = checkboxes.getValues();
var newCBValues = new Array(cbRows);
for (var row = 0; row < cbRows; row++) {
newCBValues[row] = new Array(0);
if (namesValues[row] == "" || namesValues[row] == " ") {
newCBValues[row][0] = " ";
}else{
if (cbValues[row][0] === true) {
newCBValues[row][0] = true;
}else{
newCBValues[row][0] = false;
}
}
}
checkboxes.setValues(newCBValues);
}
This part is run on the every minute Time Trigger:
function CopyandPaste() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('AA4').activate();
var currentCell = spreadsheet.getCurrentCell();
spreadsheet.getSelection().getNextDataRange(SpreadsheetApp.Direction.DOWN).activate();
currentCell.activateAsCurrentCell();
spreadsheet.getRange('O4').activate();
currentCell = spreadsheet.getCurrentCell();
spreadsheet.getSelection().getNextDataRange(SpreadsheetApp.Direction.DOWN).activate();
currentCell.activateAsCurrentCell();
spreadsheet.getRange('AA4:AA202').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
};
Column AA = TIMEVALUE(Column P)
From the question
I tried to put it in another column and have the values CopyAndPasted Values Only into a column with that run on a time trigger of every minute, but apparently that doesn't count as an Edit or it's not reading the DisplayValue.
You are right, only actions done by a user directly through the Google Sheets user interface will cause the edit trigger to be triggered.
One option is to make that your time-driven trigger besides changing the checkboxes also call the timeValue function but you have to refactor it or to mock the edit event object to pass it as the timeValue parameter.
Reference
https://developers.google.com/apps-script/guides/triggers
I managed to collect data of user typing in and by using onChange trigger, I did meet my requirements for my task. However, I realized that for example I am filling up Column A1:A4. If my A1:A3 is out of range, it will trigger the email. But if my A4 is within range, the values of A1:A3 will still trigger the email eventhough it has trigger it before. How do I make sure that the values that have been capture do not trigger the email again?
function myFunction() {
var ss = SpreadsheetApp.getActiveSheet();
currentsheet = ss.getSheetName();
//var values = ss.getRange(a1Notation)
//console.log(values);
var lastcol = ss.getLastColumn();
var vibrationtemplate = HtmlService.createTemplateFromFile("vibration");
var temperaturetemplate = HtmlService.createTemplateFromFile("temperature");
//console.log(lastcol);
if((currentsheet == 'A Vibration') || (currentsheet == 'B Vibration')){
console.log(currentsheet);
for(var i =2; i<=lastcol; i++){
var cell = ss.getRange(i,lastcol).getValues();
console.log(""+cell);
if(cell > 8){
console.log("Value is more than 8 and current value is "+cell);
vibrationtemplate.vibrate = cell;
MailApp.sendEmail("someone#gmail.com",
"Parameter Out of Range Notification",
"",{htmlBody: vibrationtemplate.evaluate().getContent()});
}
}
}
if((currentsheet == 'A Temperature') || (currentsheet == 'B Temperature')){
console.log(currentsheet);
for(var i =2; i<=lastcol; i++){
var cell = ss.getRange(i,lastcol).getValues();
console.log(""+cell);
if(cell > 80){
console.log("Value is more than 80 and current value is "+cell);
temperaturetemplate.temp = cell;
MailApp.sendEmail("someone#gmail.com",
"Parameter Out of Range Notification",
"",{htmlBody: temperaturetemplate.evaluate().getContent()});
}
}
}
}
EDIT: Latest code - Using a daily trigger and only checks at the end of the day.
function myFunction() {
const ss = SpreadsheetApp.getActiveSheet();
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
var sheetNumber = sheets.length; //Get number of sheets within Spreadsheet
var currentSheet = ss.getIndex()-1; //Get index of current sheet with 0 indexing
var vibrationtemplate = HtmlService.createTemplateFromFile("vibration"); //Create HTML for email by grabbing template from vibration.html
var temperaturetemplate = HtmlService.createTemplateFromFile("temperature"); //Create HTML for email by grabbing template from temperature.html
const vibrationlimit = 8; //Set vibrationlimit as constant equals to 8
const temperaturelimit = 80; //Set temperaturelimit as constant equals to 80
var currentDate = new Date(); //Get system Date and Time
var currentDay = currentDate.getDate(); //Extract Date from Full Date(currentDate)
var currentMonth = currentDate.getMonth() +1; //Extract Month from Full Date(currentDate), add +1 as month index start from 0.
for (var z = currentSheet ; z<sheetNumber ; ++z ){
SpreadsheetApp.setActiveSheet(sheets[z])
var lastcol = sheets[z].getLastColumn();
var lastrow= sheets[z].getLastRow();
var cellDate = sheets[z].getRange(1,lastcol).getValues();
var formattedCellDate = new Date(cellDate);
var cellDay = formattedCellDate.getDate();
var cellMonth = formattedCellDate.getMonth() + 1;
if((z==0) || (z==2)){
if((cellDay == currentDay) && (cellMonth == currentMonth)){
for(var i = 2; i<=lastrow; i++){
var scxvibrationname = sheets[z].getRange(i,1).getValues();
var vibration = sheets[z].getRange(i,lastcol).getValues();
if(vibration > vibrationlimit){
Logger.log("Vibration over 8 - Current Value is "+vibration);
vibrationtemplate.vibrate = vibration;
vibrationtemplate.scxvibration = scxvibrationname;
}
}
}
}
}
You can use PropertiesService to store the last row of the previous script run
The following code sets the start row for the for loop to the (last row+1) of the previous script run, so that only e-mails from newly added rows will be sent:
function myFunction() {
var ss = SpreadsheetApp.getActiveSheet();
currentsheet = ss.getSheetName();
//var values = ss.getRange(a1Notation)
//console.log(values);
var lastcol = ss.getLastColumn();
var lastrow=ss.getLastRow();
var vibrationtemplate = HtmlService.createTemplateFromFile("vibration");
var temperaturetemplate = HtmlService.createTemplateFromFile("temperature");
//console.log(lastcol);
if(PropertiesService.getScriptProperties().getKeys().length==0){
PropertiesService.getScriptProperties().setProperty('startRow', lastrow+1);
}
var startRow=PropertiesService.getScriptProperties().getProperty('startRow');
if((currentsheet == 'A Vibration') || (currentsheet == 'B Vibration')){
console.log(currentsheet);
//I ASSUME THAT YOU WANT TO LOOP THROUGH ALL ROWS AND NOT COLUMNS
for(var i =startRow; i<=lastrow; i++){
var cell = ss.getRange(i,lastcol).getValues();
console.log(""+cell);
if(cell > 8){
console.log("Value is more than 8 and current value is "+cell);
vibrationtemplate.vibrate = cell;
MailApp.sendEmail("someone#gmail.com",
"Parameter Out of Range Notification",
"",{htmlBody: vibrationtemplate.evaluate().getContent()});
}
}
}
...
PropertiesService.getScriptProperties().setProperty('startRow', lastrow+1);
}
function myFunction() {
const ss = SpreadsheetApp.getActiveSheet();
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
var sheetNumber = sheets.length; //Get number of sheets within Spreadsheet
var currentSheet = ss.getIndex()-1; //Get index of current sheet with 0 indexing
var vibrationtemplate = HtmlService.createTemplateFromFile("vibration"); //Create HTML for email by grabbing template from vibration.html
var temperaturetemplate = HtmlService.createTemplateFromFile("temperature"); //Create HTML for email by grabbing template from temperature.html
const vibrationlimit = 8; //Set vibrationlimit as constant equals to 8
const temperaturelimit = 80; //Set temperaturelimit as constant equals to 80
var currentDate = new Date(); //Get system Date and Time
var currentDay = currentDate.getDate(); //Extract Date from Full Date(currentDate)
var currentMonth = currentDate.getMonth() +1; //Extract Month from Full Date(currentDate), add +1 as month index start from 0.
for (var z = currentSheet ; z<sheetNumber ; ++z ){
SpreadsheetApp.setActiveSheet(sheets[z])
var lastcol = sheets[z].getLastColumn();
var lastrow= sheets[z].getLastRow();
var cellDate = sheets[z].getRange(1,lastcol).getValues();
var formattedCellDate = new Date(cellDate);
var cellDay = formattedCellDate.getDate();
var cellMonth = formattedCellDate.getMonth() + 1;
if((z==0) || (z==2)){
if((cellDay == currentDay) && (cellMonth == currentMonth)){
for(var i = 2; i<=lastrow; i++){
var scxvibrationname = sheets[z].getRange(i,1).getValues();
var vibration = sheets[z].getRange(i,lastcol).getValues();
if(vibration > vibrationlimit){
Logger.log("Vibration over 8 - Current Value is "+vibration);
vibrationtemplate.vibrate = vibration;
vibrationtemplate.scxvibration = scxvibrationname;
}
}
}
}
}
I manipulated by using daily time-driven trigger.
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);
}
I have a script that needs to run when a sheet is edited. I need it to run anytime a box in column L (L2:L260) is checked. I originally wrote the script to run on a button push (check a number of boxes, click the button and it would run), but it can't be run by other people who need to be able to use it. The script originally worked great when it was click the button, but once I added in the onEdit stuff...it completely stopped working. Here's what I have:
function onEdit(e) {
//Get the sheet you want to work with.
var editrange = {
top : 2,
bottom : 260,
left : 11,
right : 11};
var thisrow = e.range.getrow();
if (thisrow < editrange.top || thisrow > editrange.bottom)
return;
var thiscolumn = e.range.getcolumn();
if (thiscolumn < editrange.left || thiscolumn > editrange.right)
return;
var ss = e.range.getSheet();
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) {
// for (var j in rangevalues) {
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("I183:I270");
destCell8.setValues(range2values8);
//clear checkboxes
var cleaning = ss.getSheetByName('Asset Bank');
var cleaningcell8 = cleaning.getRange("A3:A90").getValues();
range8.setValues(cleaningcell8);
}
Thanks for any help I can get!
After review of your code I guess the getcloumn() you've edited in an also erroneous method getcolumn(), the
var ss = e.range.getSheet();
var sheet = ss.getSheetByName("Responsible");
and the
e.range.getrow()
seems to be the major problems
correct syntax is:
getColumn();
getRow();
and you need to use the function getSheetByName(name) out from a spreadsheet object not a sheet object.
Here is the corrected code:
function onEdit(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("I183:I270");
destCell8.setValues(range2values8);
//clear checkboxes
var cleaning = ss.getSheetByName('Asset Bank');
var cleaningcell8 = cleaning.getRange("A3:A90").getValues();
range8.setValues(cleaningcell8);
}