I'm having trouble with what seems like a very simple problem. Yet, I don't know how to fix it.
I am trying to clear cell A2 if and only if A1 is empty. I'm sure there is an easy solution but I don't seem to notice it. I'll share a sample spreadsheet for all of you to visualize my objective.
https://docs.google.com/spreadsheets/d/1qiq0w4xcUDO8pkFyeMO2ma_KR48evSsVAP6o50O0RtI/edit#gid=0
This is what I tried.
function onEdit(e) {
var sheet = SpreadsheetApp.getActive().getSheetByName("TEST");
var range = e.source.getActiveRange();
if(e.range.getRow() == 1 && e.range.getColumn() == 1) {
if(sheet.getRange("A1").getValue() == "") {
sheet.getRange('A2').clearContent();
}
}
}
Is there any problem with what I'm doing?
Help would be awesome.
The below works for me. I think you might be getting the selected range which may be different? Also you might not want to run this on other sheets? If performance becomes an issue, it might make sense to reduce the && statements and use more if statements.
function onEdit(e) {
const range = e.range;
const zzSheetName = "TEST";
const ss = range.getSheet();
if(ss.getName() == zzSheetName
&& ss.getRange("A1").isBlank()
&& range.getRow()==1
&& range.getColumn()==1
&& (!ss.getRange("A2").isBlank())
){
ss.getRange('A2').clearContent();
}
}
Clear A2 if A1 is empty
function onEdit(e) {
var sh = e.range.getSheet();
if (sh.getName() == "TEST" && e.range.columnStart == 1 && e.range.rowStart == 1 && e.value === '') {
e.range.offset(0, 1).clearContent();
}
}
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 have the following script. Its full purpose is to remove cells of columns B,C,D,E,F if the content in column A was deleted, only on the matching row. It functions as it should. However, it is a bit slow.
I couldn't figure out a way to do it myself. Which is why I am here. How could I go about adjusting it, so instead of removing the cells individually, I could instead grab a full range of them, in the matching row? Perhaps based on the offset of the initially adjusted cell.
function onEdit(e) {
if(e.range.columnStart === 1
&& e.range.rowStart > 1
&& e.range.getSheet().getName() == 'Sheet1'
&& e.range.getValue() == '') {
e.range.offset(0,1).deleteCells(SpreadsheetApp.Dimension.ROWS);
e.range.offset(0,2).deleteCells(SpreadsheetApp.Dimension.ROWS);
e.range.offset(0,3).deleteCells(SpreadsheetApp.Dimension.ROWS);
e.range.offset(0,4).deleteCells(SpreadsheetApp.Dimension.ROWS);
e.range.offset(0,5).deleteCells(SpreadsheetApp.Dimension.ROWS)
}
}
To implement a couple of additional checks for copy-paste and the like, and to observe onEdit(e) best practices, try this:
/**
* Simple trigger that runs each time the user hand edits the spreadsheet.
*
* #param {Object} e The onEdit() event object.
*/
function onEdit(e) {
if (e.range.columnStart !== 1
|| e.range.rowStart <= 1
|| e.range.getSheet().getName() !== 'Sheet1'
|| (e.value || e.range.getValue())
|| e.range.getHeight() + e.range.getWidth() > 2) {
return;
}
e.range.offset(0, 1, 1, 5).clearContent();
}
Try:
function onEdit(e) {
if(e.range.columnStart === 1
&& e.range.rowStart > 1
&& e.range.getSheet().getName() == 'Sheet1'
&& e.range.getValue() == '') {
deletecols()
}
}
function deletecols(){
var sh = SpreadsheetApp.getActive().getActiveSheet()
var range = sh.getActiveRange()
var numrows = range.getNumRows()
var row = range.getRow()
sh.getRange(row,2,numrows,5).clearContent()
}
My not be that much faster:
function onEdit(e) {
const sh = e.range.getSheet();
if(e.range.columnStart == 1 && e.range.rowStart > 1 && sh.getName() == 'Sheet1' && e.value == '') {
sh.getRange(e.range.rowStart,2,1,5).setValues([['','','','','']]);
}
}
I am using the following code to automatically turn inputs in columns 1 and 4 of a sheet into a hyperlink. Whenever any range beyond 1 cell is pasted into the sheet only the first value will be hyperlinked into all cells in the range. Is there a way to get the cells within the range so I can have it check each cell for their individual value and column?
Thanks for any help!
function onEdit(e) {
// Set a comment on the edited cell to indicate when it was changed.
let range = e.range;
let order = range.getValue()
if (!( order === ''))
{
if (range.getColumn() == 1 || range.getColumn() == 4) {range.setValue("=hyperlink(\""+order+"\", \""+order+"\")");}
}
}
If you wish to tie it down to a single sheet
function onEdit(e) {
//e.source.toast('Entry')
//Logger.log(JSON.stringify(e));
const sh = e.range.getSheet();
if (sh.getName() == "Sheet1" && e.range.rowStart > 1 && (e.range.columnStart == 1 || e.range.columnStart == 4) && e.value) {
//e.source.toast("flag1");
e.range.setValue(`=hyperlink("${e.value}","${e.value}")`);
}
}
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());
}
}
}
I have a document that when any cell from column B through G has data entered into it (starting from row 4) I want Row A to have a TimeStamp of when that data was started and to not change. But if the row is cleared I want the timestamp to be deleted and be able to be reentered with new data. And I'm using this across multiple sheets. I did the IFS function and it worked, but people were overwriting and deleting it so I am trying to script it. I've tried modifying a few scripts I've found online and using some of the stuff I remember from college days, but it hasn't been great and I could use some help. Thanks!
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSheet();
var range = ss.getRangeList(['B4:B', 'C4:C','D4:D','E4:E', 'F4:F', 'G4:G']);
if ( e.range) return;
e.targetColumn = 1
.setValue (new Date("HH:mm"));
}
function onEdit(e) {
if ( e.range.columnStart !== 2 && e.range.columnStart !== 3
&& e.range.columnStart !== 4 && e.range.columnStart !== 5
&& e.range.columnStart !== 6 && e.range.columnStart !== 7) return;
e.range.targetColumn = 1
.setValue(new Date());
}
(Updated) Try this:
function onEdit(e) {
e.source.toast('Entry');
const sh = e.range.getSheet();
if (sh.getName() == "Your sheet name" && e.range.columnStart > 1 && e.range.columnStart < 8 && e.range.rowStart > 3) {
e.source.toast('Flag1');
if(sh.getRange(e.range.rowStart,2,1,sh.getLastColumn() -1).isBlank() ) {
sh.getRange(e.range.rowStart(),1).clearContent();
} else {
if(sh.getRange(e.range.rowStart,1).isBlank()) {
sh.getRange(e.range.rowStart,1).setValue(new Date());
}
}
}
}