I am trying to get Google Sheets show a popup message whenever a cell changes from FALSE to TRUE. I have found a few ways of doing it using onEdit functions. The problem here is that the Cell in question does not really get edited, but rather changes value due to another set of cells meeting certain criteria. That means onEdit functions won't run when the value changes.
I tried this script, but it does nothing:
//popup message
function alertMessageYesNoButton() {
var result = SpreadsheetApp.getUi().alert("Are you sure you want to send an alert about?", SpreadsheetApp.getUi().ButtonSet.YES_NO);
SpreadsheetApp.getActive().toast(result);
}
function sendMessageYesNo(e){
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
var cellValue = sheet.getRange(4, 17).getValue();
if (cellValue == 'TRUE'){
alertMessageYesNoButton();
}else{}
}
If someone could come up with something that works, I would really appreciate it.
From your following reply,
cell D17 changes from FALSE to TRUE when three other checkboxes are checked using this formula: {"";SUMPRODUCT(B16:B18)=COUNTA(B16:B18)}.
In this case, how about checking the checkboxes of "B16", "B17", "B18"? When this is reflected in your showing script, how about the following modification? I thought that when those checkboxes are manually checked, onEdit trigger can be used.
In this modification, when all checkboxes of "B16:B18" of "Sheet1" are checked, your script of alertMessageYesNoButton() is run. So, when you test this, please check all checkboxes of "B16:B18" of "Sheet1". By this, the script is run.
Pattern 1:
function onEdit(e) {
const sheetName = "Sheet1"; // This is from your script.
const checkBoxRange = "B16:B18"; // This is from your reply.
const { range } = e;
const sheet = range.getSheet();
const r = sheet.getRange(checkBoxRange);
const rowStart = r.getRow();
const rowEnd = rowStart + r.getNumRows() - 1;
const colStart = r.getColumn();
const condition1 = sheet.getSheetName() == sheetName;
const condition2 = range.rowStart >= rowStart && range.rowEnd <= rowEnd && range.columnStart == colStart;
const condition3 = r.getValues().every(([b]) => b === true);
if (!condition1 || !condition2 || !condition3) return;
alertMessageYesNoButton(); // This is your script.
}
Pattern 2:
function onEdit(e) {
const sheetName = "Sheet1"; // This is from your script.
const checkBoxRange = "B16:B18"; // This is from your reply.
const { range } = e;
const sheet = range.getSheet();
const r = sheet.getRange(checkBoxRange);
const rowStart = r.getRow();
const rowEnd = rowStart + r.getNumRows() - 1;
const colStart = r.getColumn();
const condition1 = sheet.getSheetName() == sheetName;
const condition2 = range.rowStart >= rowStart && range.rowEnd <= rowEnd && range.columnStart == colStart;
const condition3 = r.getValues().every(([b]) => b === true);
if (!condition1 || !condition2 || !condition3) return;
// This is your script.
var cellValue = sheet.getRange(4, 17).getValue();
if (cellValue == 'TRUE') {
alertMessageYesNoButton();
}
}
Reference:
Simple Triggers
Added:
From your following reply,
this works very well for the example I laid out, but I tried using several multiple instances of the same script within the same sheet and it always works only in one of them. So let´s say I have for instances of the same situation (B16:B18), (B13:B15), (D13:D15) and (D16:D18). Only when I check the boxes corresponding to the last function (from top to bottom), the message pops up.
From your question and your reply, I proposed an answer as the checkboxes of "B16:B18". But from your reply, when your checkboxes are "B16:B18", "B13:B15", "D13:D15", "D16:D18", how about the following sample script?
Sample script:
function onEdit(e) {
const sheetName = "Sheet1"; // This is from your script.
const checkBoxRanges = ["B16:B18", "B13:B15", "D13:D15", "D16:D18"]; // This is from your reply.
const { range } = e;
const sheet = range.getSheet();
const actSheetName = sheet.getSheetName();
const res = checkBoxRanges.find(checkBoxRange => {
const r = sheet.getRange(checkBoxRange);
const rowStart = r.getRow();
const rowEnd = rowStart + r.getNumRows() - 1;
const colStart = r.getColumn();
const condition1 = actSheetName == sheetName;
const condition2 = range.rowStart >= rowStart && range.rowEnd <= rowEnd && range.columnStart == colStart;
const condition3 = r.getValues().every(([b]) => b === true);
return condition1 && condition2 && condition3;
});
if (!res) return;
Browser.msgBox(`Checked checkboxes of ${res}`); // Here, you can see the checkbox group that was checked.
alertMessageYesNoButton(); // This is your script.
}
In this case, by giving const checkBoxRanges = ["B16:B18", "B13:B15", "D13:D15", "D16:D18"], the script detects the checked checkboxe group, and show the range as a sample.
Related
I have this code which works for cells A2-A4:
//The function onEdit ensures that checkboxes deleted (by mistake) in the sheet are immediately re-created.
function onEdit(e) {
var spreadsheet = SpreadsheetApp.getActive();
if(spreadsheet.getSheetName()=='MySheet') { //to avoid executing for another sheet
var checkboxCells = [
'A2','A3','A4'];
var range = e.range;
var value = range.getValue();
var a1Notation = range.getA1Notation();
if (checkboxCells.indexOf(a1Notation) != -1 && value != 'TRUE' && value != 'FALSE') {
range.insertCheckboxes();
}
}
};
However I need to work with a 1D range (since there are many cells) and hence I am trying to use this:
//The function onEdit ensures that checkboxes deleted (by mistake) in the sheet are immediately re-created.
function onEdit(e) {
var spreadsheet = SpreadsheetApp.getActive();
if(spreadsheet.getSheetName()=='MySheet') { //to avoid executing for another sheet
var checkboxCells = spreadsheet.getSheetByName('MySheet')
.getRange('MyRange').getA1Notation();
var range = e.range;
var value = range.getValue();
var a1Notation = range.getA1Notation();
if (checkboxCells.indexOf(a1Notation) != -1 && value != 'TRUE' && value != 'FALSE') {
range.insertCheckboxes();
}
}
};
I think this second version should work, but it does not.
Why does it not work?
Modification points:
In order to check whether the edited cell is included in the specific range you expect, the following sample script can be used. (var { range } = e;)
var checkboxCells = sheet.getRange('MyRange');
var startRow = checkboxCells.getRow();
var endRow = startRow + checkboxCells.getNumRows() - 1;
var startCol = checkboxCells.getColumn();
var endCol = startCol + checkboxCells.getNumColumns() - 1;
var check = range.rowStart >= startRow && range.rowEnd <= endRow && range.columnStart >= startCol && range.columnEnd <= endCol;
In order to check whether the edited cell is the checkbox, isChecked() can be used. In this case, when the cell is the checkbox, true or false are returned. When the cell is not the checkbox, null is returned. I thought that this might be able to be used.
When these points are reflected in your script, how about the following modification?
Modified script:
Please set your sheet name and your range.
function onEdit(e) {
var { range } = e;
var sheet = range.getSheet();
if (sheet.getSheetName() == 'MySheet') {
var checkboxCells = sheet.getRange('MyRange');
var startRow = checkboxCells.getRow();
var endRow = startRow + checkboxCells.getNumRows() - 1;
var startCol = checkboxCells.getColumn();
var endCol = startCol + checkboxCells.getNumColumns() - 1;
var check = range.rowStart >= startRow && range.rowEnd <= endRow && range.columnStart >= startCol && range.columnEnd <= endCol;
if (check && range.isChecked() === null) {
range.insertCheckboxes();
}
}
}
In this modified script, when a cell is edited, when the edited cell is included in MyRange, it checks whether the edited cell is a checkbox when the edited cell is not a checkbox, the checkboxes are inserted into the edited cell.
Reference:
isChecked()
The goal is to create a script that automatically adds strikethrough to text across a row when the last cell in a row's value is changed to "FOUND!"
This is what I have so far:
function strikethroughRow(row) {
const ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Records");
const firstCell = "B" + row;
const secondCell = "A" + row;
firstCell.setFontLine("line-through");
secondCell.setFontLine("line-through");
}
function onEdit(e) {
const column = e.getColumn();
const value = e.getValue();
if(column === 3.0 && value === "FOUND!"){
const row = e.getRow();
strikethroughRow(row);
}
}
From your question and your showing script, I believe your goal is as follows.
When a value of "FOUND!" is put into column "C", you want to set the strikethrough to columns "A" and "B".
Modification points:
In your script, e.getColumn(), e.getValue() and e.getRow() are not correct. In this case, it is required to use e.range.
firstCell and secondCell are string value. By this, an error occurs.
I thought that in this case, the script might be able to be simpler. So, how about the following modification?
Modified script:
function onEdit(e) {
const sheetName = "Sheet1"; // Please set your sheet name.
const { range } = e;
const sheet = range.getSheet();
const value = range.getValue();
if (sheet.getSheetName() != sheetName || range.columnStart != 3 || value != "FOUND!") return;
sheet.getRange(range.rowStart, 1, 1, sheet.getLastColumn() - 1).setFontLine("line-through");
}
Note:
If the last column is not column "C", how about the following modified script?
function onEdit(e) {
const sheetName = "Sheet1"; // Please set your sheet name.
const { range } = e;
const sheet = range.getSheet();
const value = range.getValue();
const lastColumn = sheet.getLastColumn();
if (sheet.getSheetName() != sheetName || range.columnStart != lastColumn || value != "FOUND!") return;
sheet.getRange(range.rowStart, 1, 1, sheet.getLastColumn() - 1).setFontLine("line-through");
}
Reference:
Simple Triggers
I am trying to create a script to sort a table on the current active sheet/tab but I am getting an error that I could not identify.
Any help is appreciated!
function onEdit(e) {
var sht = e.source.getActiveSheet();
var arr_sht = ["sheet 1","sheet 2","sheet 3","sheet 4"]
var sht_name = sht.getName();
if (!arr_sht.includes(sht_name)){return;};
var c = e.range.getColumn();
if(c !== 1) {return;}
var r = e.range.getRow();
if (r<12) {return;}
if (e.value !== 'next step'){return};
var rng = SpreadsheetApp.getActiveSheet().getDataRange();
rng = rng.offset(11,0,rng.getNumRows()-1);
rng.sort({column:1,ascending:true})
}
Use Sheet.getRange(), like 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) {
throw new Error(
'Please do not run the onEdit(e) function in the script editor window. '
+ 'It runs automatically when you hand edit the spreadsheet. '
+ 'See https://stackoverflow.com/a/63851123/13045193.'
);
}
const sheetRegex = /^(sheet 1|sheet 2|sheet 3|sheet 4)$/i;
const rowStart = 11;
const columnStart = 1;
let sheet;
if (e.value !== 'next step'
|| e.range.rowStart < rowStart
|| e.range.columnStart !== columnStart
|| !(sheet = e.range.getSheet()).getName().match(sheetRegex)) {
return;
}
const table = sheet.getRange(rowStart, columnStart, sheet.getLastRow() - rowStart + 1, sheet.getLastColumn() - columnStart + 1);
table.sort({ column: columnStart, ascending: true })
}
See these onEdit(e) optimization tips.
Try this:
function onEdit(e) {
const sh = e.range.getSheet();
const names = ["sheet 1","sheet 2","sheet 3","sheet 4"]
const idx = names.indexOf(sh.getName());
if(~idx && e.range.columnStart == 1 && e.range.rowStart < 12 && e.value == 'next step') {
sh.getDataRange().sort({column:1,ascending:true})
}
}
How do I make a positive value be converted automatically into negative without adding formula to a cell when someone pastes/or added a value to a cell.
In this example I want that 5 2 and 3 be a negative value.
In your situation, how about using the simple trigger of onEdit as follows?
Sample script:
Please copy and paste the following script to the script editor of Spreadsheet and save the script. When you use this script, please put a positive value to a cell. By this, the positive value is converted to the negative value.
function onEdit(e) {
const value = e.range.getValue();
if (!isNaN(value) && value > 0) {
e.range.setValue(-value);
}
}
Note:
If you want to limit the sheet and the range, please tell me.
If you want to convert all cell values in a sheet by a script, how about the following script? When this script is run, the positive values in a sheet are converted to negative values.
function myFunction() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1"); // Please set your sheet name.
const range = sheet.getDataRange();
const values = range.getValues().map(r => r.map(c => !isNaN(c) && c > 0 ? -c : c));
range.setValues(values);
}
Reference:
Simple Triggers
Added 1:
From your following additional request,
Is it possible to apply it with certain range only. Like to cell C45:AK76 and C84:AK115?
In this case, how about the following sample script?
Sample script:
function onEdit(e) {
const ranges = ["C45:AK76", "C84:AK115"]; // These ranges are from your reply.
const sheetName = "Sheet1"; // Please set your sheet name.
const { range } = e;
const sheet = range.getSheet();
const value = range.getValue();
const check = sheet.getRangeList(ranges).getRanges().some(r => {
const startRow = r.getRow();
const endRow = startRow + r.getNumRows() - 1;
const startCol = r.getColumn();
const endCol = startCol + r.getNumColumns() - 1;
return range.rowStart >= startRow && range.rowEnd <= endRow && range.columnStart >= startCol && range.columnEnd <= endCol;
});
if (sheet.getSheetName() != sheetName || !check || isNaN(value) || value < 0) return;
range.setValue(-value);
}
In this sample, the script is run when the cells "C45:AK76", "C84:AK115" are edited.
Added 2:
From your following additional request,
Im wondering if I can apply this to two other sheets with the same cell range?
In this case, how about the following sample script?
Sample script:
function onEdit(e) {
const ranges = ["C45:AK76", "C84:AK115"]; // These ranges are from your reply.
const sheetNames = ["Sheet1", "Sheet2",,,]; // Please set your sheet names.
const { range } = e;
const sheet = range.getSheet();
const value = range.getValue();
const check = sheet.getRangeList(ranges).getRanges().some(r => {
const startRow = r.getRow();
const endRow = startRow + r.getNumRows() - 1;
const startCol = r.getColumn();
const endCol = startCol + r.getNumColumns() - 1;
return range.rowStart >= startRow && range.rowEnd <= endRow && range.columnStart >= startCol && range.columnEnd <= endCol;
});
if (!sheetNames.includes(sheet.getSheetName()) || !check || isNaN(value) || value < 0) return;
range.setValue(-value);
}
Added 3:
From your following additional request,
If I paste for example these 3 different values into the cell., example the number 4 5 and 6. Instead of just having -4,-5, and -6. The result I'm getting is -4,-4 and -4.
In this case, how about the following sample script?
Sample script:
function onEdit(e) {
const ranges = ["C45:AK76", "C84:AK115"]; // These ranges are from your reply.
const sheetNames = ["Sheet1", "Sheet2",,,]; // Please set your sheet names.
const { range } = e;
const sheet = range.getSheet();
const values = range.getValues();
const check = sheet.getRangeList(ranges).getRanges().some(r => {
const startRow = r.getRow();
const endRow = startRow + r.getNumRows() - 1;
const startCol = r.getColumn();
const endCol = startCol + r.getNumColumns() - 1;
return range.rowStart >= startRow && range.rowEnd <= endRow && range.columnStart >= startCol && range.columnEnd <= endCol;
});
if (!sheetNames.includes(sheet.getSheetName()) || !check) return;
range.setValues(values.map(r => r.map(c => (!isNaN(c) && c > 0) ? -c : c)));
}
I have a google sheet with a column full of checkboxes. My goal is to have a row of the selected adjacent columns of data (namedInsured, lineCoverage, etc.) append to a separate sheet when a checkbox is selected and True. This is what I currently have, and it does absolutely nothing.
function onEdit(e) {
const ss = e.range.getSheet();
var active_range = ss.getActiveRange();
var namedInsured = ss.getRange(active_range.getRowIndex(), 4).getValue();
var lineCoverage = ss.getRange(active_range.getRowIndex(), 5).getValue();
var effDate = ss.getRange(active_range.getRowIndex(), 6).getValue();
var assignedTo = ss.getRange(active_range.getRowIndex(), 15).getValue();
var data = [namedInsured, lineCoverage, effDate, assignedTo];
const destinationSheet = ss.getSheetByName("Sheet4");
if (ss.getName() != "Sheet4" && e.range.columnStart == 10 && e.value == "TRUE")
{
e.source.toast("Quote In Process Entered")
destinationSheet.appendRow(data);
}
}
[This is an example of the desired output]
<< If the first and fourth checkboxes were selected one after the other, this is how they should appear on the separate sheet
This works for me
function onEdit(e) {
e.source.toast('Entry')
const sh = e.range.getSheet();
if (sh.getName() == "Sheet0" && e.range.columnStart == 10 && e.value == "TRUE") {
e.source.toast('Flag1');
const [a,b,c] = sh.getRange(e.range.rowStart, 4,1,3).getValues()[0];
var d = sh.getRange(e.range.rowStart, 15).getValue();
var data = [a, b, c, d];
const dsh = e.source.getSheetByName("Sheet1");//modified from original
dsh.appendRow(data);
}
}
Demo: