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);
}
}
}
Related
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");
}
}
I am using Scripts to create a multi- select dropdown using onEdit as the function. The issue I have is that it was initially working perfectly & executing in 0.15s but has now started timing out after 30 seconds. I'm not sure how to adapt this as I don't know what the change is. Any help would be appreciated!
function MultiSelect(e) {
var oldValue;
var newValue;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var activeCell = ss.getActiveCell();
if (
activeCell.getColumn() == 2 &&
ss.getActiveSheet().getName() == 'Waitlist'
) {
newValue = e.value;
oldValue = e.oldValue;
if (!e.value) {
activeCell.setValue('');
} else {
if (!e.oldValue) {
activeCell.setValue(newValue);
} else {
activeCell.setValue(oldValue + ', ' + newValue);
}
}
}
}
Script image
Try it this way:
function MultiSelect(e) {
if (e.range.columnStart == 2 && e.range.getSheet().getName() == 'Waitlist') {
if (!e.value) {
e.range.setValue('');
} else if (!e.oldValue) {
e.range.setValue(e.value);
} else {
e.range.setValue(e.oldValue + ', ' + e.value);
}
}
}
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();
}
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}`);
}
}
}
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