onEdit(e) trigger does nothing when cell is being edited - google-apps-script

I am trying to change the text in a cell (different from the edited one) when a specific cell is being edited
I am using the following code:
function get_cells(){
if (sheet.getRange(3,6).getValue() == 'Cat') {
sheet.getRange(5,5).setValue('Animal')
} else if (sheet.getRange(3,6).getValue() == 'Apple') {
sheet.getRange(5,5).setValue('Fruit')
} else {
sheet.getRange(5,5).setValue(NaN)
}
}
function onEdit(e){
var range = e.range;
if (range.getRow()== 3 && range.getColumn()==6){
get_cells();
}
}
The idea is that when the cell (3,6) is edited then the content of the cell (5,5) will be changed automatically. But it doesn't work

Try it this way:
Get cells required the sheet. You could have learned this by viewing you executions.
function get_cells(sheet) {
if (sheet.getRange(3, 6).getValue() == 'Cat') {
sheet.getRange(5, 5).setValue('Animal')
} else if (sheet.getRange(3, 6).getValue() == 'Apple') {
sheet.getRange(5, 5).setValue('Fruit')
} else {
sheet.getRange(5, 5).setValue(NaN)
}
}
function onEdit(e) {
e.source.toast('entry');
const sheet = e.range.getSheet();
if (e.range.rowStart == 3 && e.range.columnStart == 6) {
e.source.toast('first if')
get_cells(sheet);
}
}
Incomplete list of event object values

Related

Delete cell if cell C7 is empty

I'm working with Google Apps Script and I am trying to write a simple code (I believe it is going to be simple....)
So what I am trying to achieve is the following:
If cell C7 is empty, I want cell A20:A to clear.
I have tried
.isBlank()
"C7" === ""
but none of them seems to work. Can anyone help me solve it? I learned python but apparently Java seems to not function the same way.
Maybe a little help please?
My trial code is this. I think nothing is wrong but why won't it work?
function myFunction() {
var sheet = SpreadsheetApp.getActive().getSheetByName("sheet1");
if(sheet.getRange(7, 3).getValue() == ''){
sheet.getRange("A20:A").removeCheckboxes();
}
}
Okay. I decided to share the entire onEdit(e) function I have right now.
function onEdit(e) {
first();
second();
function first() {
const sheet = SpreadsheetApp.getActiveSheet();
const sName = sheet.getName();
if (sName === "sheet1") {
const len = sheet.getRange('B20:B').getValues().filter(row => row[0] != '').length;
if (e.range.getColumn() === 3 && e.range.getRow() === 7) {
const range = sheet.getRange(20,1,len,1);
sheet.getRange('A20:A').removeCheckboxes();
range.insertCheckboxes().uncheck();
}
if (e.range.getRow() > 19 && e.range.getColumn() === 1 && e.range.getValues()[0][0] === true) {
sheet.getRange('A20:A').uncheck();
e.range.check();
}
}
}
function second() {
var sheet = SpreadsheetApp.getActive().getSheetByName("sheet1");
var range = e.source.getActiveRange();
if(e.range.getRow() == 7 && range.getColumn() == 3) {
if(sheet.getRange("C7").getValue() == "") {
Logger.log("C7 is empty");
sheet.getRange('A20:A').clearContent();
}
}
}
}
A very dear person from stackoverflow has helped me right the first half of the function. Thank you and shout out to dear friend!!
Second part of the function I very much referenced dear Mateusz. Thank you so much for the help.
However, I still have a problem of not being able to clear the cell whenever cell C7 is empty. Can anyone spot the problem???
You can do it like this:
function myFunction() {
var sheet = SpreadsheetApp.getActive().getSheetByName("sheet1");
if(sheet.getRange(7, 3).getValue() == '')
{
Logger.log("C7 is empty");
sheet.getRange(20,1,sheet.getLastRow(),1).clearContent();
}else{
Logger.log("C7 is not empty");
}
}
code for checking every time C7 cell is edited:
function onEdit(e) {
var sheet = SpreadsheetApp.getActive().getSheetByName("sheet1");
var range = e.source.getActiveRange();
if(e.range.getRow() == 7 && range.getColumn() == 3)
{
if(sheet.getRange(7, 3).getValue() == '')
{
Logger.log("C7 is empty");
sheet.getRange(20,1,sheet.getLastRow(),1).clearContent();
}else{
Logger.log("C7 is not empty");
}
}
}
code to check ecery time a cell is edited:
function onEdit(e) {
var sheet = SpreadsheetApp.getActive().getSheetByName("sheet1");
if(sheet.getRange(7, 3).getValue() == '')
{
Logger.log("C7 is empty");
sheet.getRange(20,1,sheet.getLastRow(),1).clearContent();
}else{
Logger.log("C7 is not empty");
}
}

How to trigger a macro in Google Sheets when a cell is not blank?

I have a drop-down menu of values from a database. Each of these values has a numeric code that will populate in a validation cell as long as the value has not been edited. If the value has been edited, the validation cell is blank.
I would like to know when a value has been selected and then edited. To do this, I would like to set up a trigger that copies the code from the validation cell every time it changes to another code - but not when it is made blank. This way, I will have a record of the original value that was selected before it was changed.
Here is the code I've tried so far:
function Untitledmacro() {
var spreadsheet = SpreadsheetApp.getActive();
if('O26' !== "") {
spreadsheet.getRange('P22').activate();
spreadsheet.getRange('O26').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
}
};
function onEdit(e) {
if(e.range.getA1Notation() == 'H26'){
Untitledmacro()
}
}
And
function Untitledmacro() {
var spreadsheet = SpreadsheetApp.getActive();
if('O26' !== 0) {
spreadsheet.getRange('P22').activate();
spreadsheet.getRange('O26').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
}
};
function onEdit(e) {
if(e.range.getA1Notation() == 'H26'){
Untitledmacro()
}
}
Cell H26 is the drop-down menu
Cell O26 is the validation cell with the numeric code
Cell P22 is where the numeric code is copied to
In both cases above, the macro will run correctly when cell H26 is edited. But it does so whether cell O26 is blank or not.
Any help here would be greatly appreciated
UPDATE
This code is working thanks to Cooper:
function onEdit(e) {
e.source.toast("Entry")
const sh = e.range.getSheet();
if (sh.getName() == "Response Builder - Erin" && e.range.columnStart == 8 && e.range.rowStart == 26 && e.value) {
e.source.toast("Flag1")
let v = sh.getRange("O26").getValue();
if (v !== "") {
sh.getRange("R26").setValue(sh.getRange("O26").getValue());
}
}
}
If it is possible, I would like to make this work for a range of data. I would like for it to copy non-blank entries from O26:65 into R26:R65 when H26:H65 is edited.
Try this:
function onEdit(e) {
e.source.toast("Entry")
const sh = e.range.getSheet();
if (sh.getName() == "Enter Your Sheet Name" && e.range.columnStart == 8 && e.range.rowStart == 26 && e.value) {
e.source.toast("Flag1")
let v = sh.getRange("O26").getValue();
if (v !== "") {
sh.getRange("P22").setValue(sh.getRange("O26").getValue());
sh.getRange("O26").copyTo(sh.getRange(e.range.rowStart,e.range.columnStart));
}
}
}
Final Version Modified by OP:
function onEdit(e) {
e.source.toast("Entry")
const sh = e.range.getSheet();
if (sh.getName() == "Response Builder - Erin" && e.range.columnStart == 8 && e.range.rowStart == 26 && e.value) {
e.source.toast("Flag1")
let v = sh.getRange("O26").getValue();
if (v !== "") {
sh.getRange("R26").setValue(sh.getRange("O26").getValue());
}
}
}

Trigger a funtion when a check box is true - google sheet script [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 months ago.
Improve this question
I am trying to trigger a function if the (D1) checkbox is true
Instead of a button, I need a checkbox. Button click works only on desktop but not on Mobile/Tablet.
I tried referring to this similar post. But still the script is not running
Calling function when checking checkbox - Google Sheets
this code is not working for me. Please guide.
function onEdit() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
var range = sheet.getActiveRange()
if (range.isChecked()) {
if (range.getA1Notation() == "D1") {
searchData()
} else if (range.getA1Notation() == "E1") {
submitData()
}
range.uncheck()
}
}
for your reference, below is the function I am trying to trigger/call
**// Please run this function if the D1 is true.**
function searchdata() {
const srcSpreadsheetId = "1QL0jaNts2YRkZTlxmS0bk7V1fVVHBsJFmxS5C05PEmA";
const srcSheetName = "DataSheet";
const dstSheetName = "UserForm";
// Retrieve values from source sheet and create an array and search value.
const dstSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
const dstSheet = dstSpreadsheet.getSheetByName(dstSheetName);
const search = dstSheet.getRange("B1").getValue();
// Search the value.
const srcSpreadsheet = SpreadsheetApp.openById(srcSpreadsheetId);
const srcSheet = srcSpreadsheet.getSheetByName(srcSheetName);
const range = srcSheet.getRange("A2:A" + srcSheet.getLastRow()).createTextFinder(search).findNext();
if (!range) {
SpreadsheetApp.getUi().alert('UserForm Number Not Found');
}
also, this is the working file link https://docs.google.com/spreadsheets/d/1NY_ckzEWxU7DCGro5tTqzpiOi6iG5PAQFxpZg0OKodY/edit?usp=sharing
Try this:
function onEdit(e) {
const sh = e.range.getSheet();
if(sh.getName() == "Your sheet name" && e.range.rowStart == 1 && e.value == "TRUE") {
if(e.range.columnStart == 4) {
searchData();
}
if(e.range.columnStart == 5) {
submitData();
}
e.range.setValue("FALSE");
}
}
This works for me
function onMyEdit(e) {
const sh = e.range.getSheet();
if(sh.getName() == "Sheet0" && e.range.rowStart == 1 && e.value == "TRUE") {
if(e.range.columnStart == 4) {
e.source.toast("D1")
//searchData();
}
if(e.range.columnStart == 5) {
e.source.toast("E1")
//submitData();
}
e.range.setValue("FALSE");
}
}
function createInstallableTrigger(funcname="onMyEdit") {
if(ScriptApp.getProjectTriggers().filter(t => t.getHandlerFunction() == funcname).length == 0 ) {
ScriptApp.newTrigger().forSpreadsheet(SpreadsheetApp.getActive()).onEdit().create();
}
}
Try this:
function onEdit(e) {
var range = e.range;
var activeSheet = e.source.getActiveSheet();
if (activeSheet.getName() != "UserForm") return;
if (range.isChecked()) {
if (range.getA1Notation() == "D1") {
searchData();
} else if (range.getA1Notation() == "E1") {
submitData();
}
range.uncheck();
}
}
When using the onEdit() function, make sure to take advantage of the on edit event object (e) as it can save you from potential bugs.
You might also want to reduce the amount of nested if statements by using this code:
function onEdit(e) {
var range = e.range;
var activeSheet = e.source.getActiveSheet();
if (activeSheet.getName() != "UserForm") return;
if (!range.isChecked()) return;
if (range.getA1Notation() == "D1") {
searchData();
} else if (range.getA1Notation() == "E1") {
submitData();
}
range.uncheck();
}

applying appscript code for multiple selections in a drop down to multiple sheets in work book

Non-coder here. I have found/copied appscript code to enable multiple selections from a drop down list in a Sheet. I've adjusted the code for the Sheet I am referring to and the relevant Columns and it works fine in that Sheet.
How or where do I apply the same basic code to other Sheets in the book while assigning it to different columns on each sheet (e.g. Sheet 1 needs this in Column 6, 5; but Sheet 2 needs this in Column 4, 5)?
This is the code I'm using
function onEdit(e) {
var oldValue;
var newValue;
var ss=SpreadsheetApp.getActiveSpreadsheet();
var activeCell = ss.getActiveCell();
if(activeCell.getColumn() == 6 || 7 &&
ss.getActiveSheet().getName()=="Sheet1")
newValue=e.value;
oldValue=e.oldValue;
if(!e.value) {
activeCell.setValue("");
}
else {
if (!e.oldValue) {
activeCell.setValue(newValue);
}
else {
if(oldValue.indexOf(newValue) <0) {
activeCell.setValue(oldValue+', '+newValue);
}
}
}
}
}
function onEdit(e) {
//e.source.toast('entry');
const obj = {'Sheet1':{low:4,high:7},'Sheet2':{low:3,high:6}};
const sh = e.range.getSheet();
//sh.getRange('A1').setValue(JSON.stringify(e));
const name = sh.getName();
if( e.range.columnStart > obj[name].low && e.range.columnStart < obj[name].high ) {
//e.source.toast('flag0');
if(!e.value) {
//e.source.toast('flag1');
e.range.setValue('');
} else if(!e.oldValue) {
//e.source.toast('flag2');
e.range.setValue(e.value);
} else if(!~e.oldValue.indexOf(e.value)){
//e.source.toast('flag3');
e.range.setValue(`${e.oldValue},${e.value}`);
}
}
}

How to restrict edit script to run in specific column _google sheets

I am running a script in sheets to allow multiple selection. The script works but when i go to edit a cell somewhere else on the sheet the information disappears. I need to run this script only for column 5, so if something is documented elsewhere, it wont keep disappearing.
function onEdit(e) {
var oldValue;
var newValue;
var ss=SpreadsheetApp.getActiveSpreadsheet();
var activeCell = ss.getActiveCell();
if(activeCell.getColumn() == 5 && ss.getActiveSheet().getName()=="TRACKER- Relaunch")
newValue=e.value;
oldValue=e.oldValue;
if(!e.value) {
activeCell.setValue("");
}
else {
if (!e.oldValue) {
activeCell.setValue(newValue);
}
else {
if(oldValue.indexOf(newValue) <0) {
activeCell.setValue(oldValue+','+newValue);
}
else {
activeCell.setValue(oldValue);
}
}
}
}
Wrap your function in an if statement which checks that the changed cell is in the 5th columns and that you are in the correct sheet.
function onEdit( event ){
const columnOfCell = event.columnStart;
const currentSheetName = SpreadsheetApp.getActiveSheet().getName();
const targetSheetName = "TRACKER- Relaunch";
if( columnOfCell === 5 && currentSheetName === targetSheetName){
// work your magic here
}
}
Try this:
function onEdit(e) {
var sh=e.range.getSheet();
if(e.range.columnStart==5 && sh.getName()=="TRACKER- Relaunch") {
if(!e.value) {//this doesn't make much sense
e.range.setValue("");
}else if(!e.oldValue) {//nor does this
e.range.setValue(e.value);
}else if(e.oldValue.indexOf(e.value)==-1) {
e.range.setValue(e.oldValue + ',' + e.value);
}else{
e.range.setValue(e.oldValue);
}
}
}