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");
}
}
Related
I am trying to run a Google App script that unhides columns, and then rehides columns based whether the cell reference says Blank or not. This is determined by a drop down in a single cell. I got the script to run fine (if a little clunky) whenever any edit is made, but when I try to add the specification of cell it flags an error with the range.
I'm very new to this so I'm a bit out of my depth so any help is appreciated!
function onEdit(e) {
if (e.range.getA1Notation() === 'B2') {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActive().getSheetByName("Group M0 loot");
sheet.showColumns(9, 35);
var range = sheet.getRange("4:4");
var num_cols = range.getNumColumns();
for (var i = 9; i <= num_cols; i++) {
var value = sheet.getRange(4,i).getValue();
if (value == "Blank") {
sheet.hideColumns(i);
};
}
}
}
Try this:
function onEdit(e) {
const sh = e.range.getSheet();
if (sh.getName() == "Group M0 loot" && e.range.columnStart == 2 && e.range.rowStart == 2) {
sh.showColumns(9, 35);
sh.getRange(4,1,1,sh.getLastColumn()).getValues().flat().forEach((c,i) => {
if(!c) {
sh.hideColumns(i + 1);
}
});
}
}
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());
}
}
}
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();
}
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);
}
}
}
I am using the following code to enable multi-select entry in two columns AK and AN in my google sheet.
function onEdit(e) {
var oldValue;
var newValue;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var activeCell = ss.getActiveCell();
if (activeCell.getColumn() == 37 || 40 && ss.getActiveSheet().getName() == "Project Tracker") {
newValue = e.value;
oldValue = e.oldValue;
if (!e.value) {
activeCell.setValue("");
} else {
if (!e.oldValue) {
activeCell.setValue(newValue);
} else {
activeCell.setValue(oldValue + ', ' + newValue);
}
}
}
}
My problem is that the script seems to be executed upon edit of any cell in my worksheet "Project Tracker" and not only the two columns (AK and AN) I configured in my script. Any help would be gladly appreciated. Thanks
Maybe you could consider modifying to the following:
function onEdit(e) {
var sheet = e.range.getSheet();
if (sheet.getName() === "Project Tracker") {
if (e.range.getColumn() == 37 || e.range.getColumn() == 40){
// Do something...
}
}
}
you could also combine the If statements as you initially did:
if(e.source.getSheetName() == "Project Tracker" &&
e.range.getColumn() == 37 ||
e.range.getColumn() == 40){
// Do something...