CheckBox - Once Checked you cannot uncheck ? - GoogleAppsScript / GoogleSheets - google-apps-script

Is it possible to code a checkbox that once checked it stays checked ?
My code would run again if a person checked - then unchecked - then accidently checked again, something I dont want to happen.
Here is the code
function onEdit(e) {
AddPalletCount(e)
AddCheckBox(e)
}
function AddCheckBox(e){
var row = e.range.getRow();
var col = e.range.getColumn();
if(col === 2 && row > 2 && e.source.getActiveSheet().getName() === "PREPSHEET"){
e.source.getActiveSheet().getRange(row,4).setValue(new Date());
e.source.getActiveSheet().getRange(row,3).setValue("Done");
var sheet = SpreadsheetApp.getActiveSheet();
var activeRange = sheet.getActiveRange();
var criteria = SpreadsheetApp.DataValidationCriteria.CHECKBOX;
var rule = SpreadsheetApp.newDataValidation().requireCheckbox().build();
var range = sheet.getRange(activeRange.getRow(), 5);
range.setDataValidation(rule);
}}
Thanks In Advance

A checkbox that stay's check once it's checked
It doesn't actually stay checked but once you check and try to uncheck it, it rechecks itself. It has to change in order to get the onEdit trigger.
Also note that this has to be an installable trigger because simple triggers can't do things that require permission like changing user data.
function onMyEdit(e) {
//e.source.toast('Entry');//debug
var sh=e.range.getSheet();
//var log=Utilities.formatString('oldValue: %s value: %s',e.oldValue,e.value);//debug
//sh.getRange(sh.getRange(1,9,sh.getLastRow(),1).getValues().filter(String).length+1,9).setValue(log);//debug
if(e.range.columnStart==2 && e.range.rowStart>2 && sh.getName()=='DONATIONS RECEIVED') {
//e.source.toast('Access');//debug
if(e.oldValue=='true' && e.value=='FALSE') {
//e.source.toast('SetValue');//debug
e.range.setValue("TRUE");
}
}
}
You might wish to keep lines 4 and 5 for future onEdit debugging work as I found them quite helpful in solving this problem.

Related

Google Sheets: Append then Delete rows based on Tickbox condition

I'm attempting to create a spreadsheet to organise products ordered at my workplace.
When an order is received a team member would add the details to the sheet; when it is collected they'd fill out date and ID then tick the order complete. See Attached
What I want to happen next is that the row containing the complete details from that order is appended to a second page in the sheet and the original row is deleted.
I can't make sense of how to get this to run automatically when the box is checked; so far I have been compiling a script to run from a button press:
function runFiling() {
function moveRows() {
var ss = SpreadsheetApp.getActive();
var osh = ss.getSheetByName('Current');
var dsh = ss.getSheetByName('Collected');
var srg = osh.getDataRange('H2:H');//You might want to specify a more unique range. This just gets all of the data on the sheet
var svA = srg.getValues();
var d=0;//deleted row counter
for(var i=1;i<svA.length;i++) {
if(svA[i][7] =='TRUE') {
dsh.appendRow(svA[i]);//append entire row to Sheet2
osh.deleteRow(i-d+1);//accounts for the difference between length of array and number of remaining row.
d++;
}
}
}
}
However even this fails to Append or Delete anything although no errors are found/returned.
If anyone can suggest a way to fix the above or, preferably, how to make the script work when the box is ticked your help will be most appreciated.
Try it this way using an onEdit(e) function
function onEdit(e) {
const sh = e.range.getSheet();
if (sh.getName() == 'Current' && e.range.columnStart == 7 && e.range.rowStart > 1 && e.value == "TRUE") {
const dsh = ss.getSheetByName('Collected');
const vs = sh.getRange(e.range.rowStart, 1, 1, sh.getLastColumn()).getValues()
dsh.getRange(dsh.getLastRow() + 1, 1, vs.length, vs[0].length).setValues(vs);
sh.deleteRow(e.range.rowStart);
}
}
This will accomplish the task line by line as the check boxes are checked off by the user.

Hide columns based on checkbox value

I would like to implement a code to my production plan. Is there a way to hide column based on the checkbox true value? Lets say I added the checkboxes to the row 5, and I want to hide each column starting from M column separately upon ticking the checkbox. I implemented the code to hide the rows, but for columns it's getting tricky since I'm not experienced in google script.
Would it be perhaps also possible to add custom filter to show all hidden columns at once, lets say if I wanna make changes in one of them, and then hide all of them at once?
Hide columns starting with M
function onEdit(e) {
e.source.toast("Entry")
const sh = e.range.getSheet();
if (sh.getName() == "Enter Your Sheet Name" && e.range.columnStart > 12 && e.range.rowStart == 5 && e.value == "TRUE") {
e.source.toast("Flag1");
sh.hideColumns(e.range.columnStart);
e.range.setValue("FALSE");
}
}
Yes, these is! If you use an onEdit function, you can use e.range.isChecked() to see if a checkbox was checked. After that, you'd just need to run activeSheet.hideColumns(editedColumn, 1) to hide the column.
This code should do the trick:
function onEdit(e) {
var activeSheet = SpreadsheetApp.getActiveSheet();
var editedRange = e.range;
var editedRow = editedRange.getRow();
var editedColumn = editedRange.getColumn();
//If the checkbox isn't checked, return
if (!editedRange.isChecked()) return;
//If the edited row isn't in row 5, return
if (editedRow != 5) return;
//If the edited column is before M, return
if (editedColumn < 12) return;
//Hide the edited column
activeSheet.hideColumns(editedColumn, 1);
}

Google Sheets onEdit - trying to copy a row to a new sheet by changing the value of a column within that same row

Please see Sheet1 of my spreadsheet:
https://docs.google.com/spreadsheets/d/1EoOIQxWyKWOvtlCrmJNI76FAxGhzgXrE4s0F05tw2MY/edit#gid=0
As an example, I would like to copy the values (not formulas) of A3:G3 to the bottom row of the 'Master' sheet by changing the value in H3 to "submitResponse." Then, once that row is copied, I would like to automatically clear out cell H3, as well as range A2:G2 (the row ABOVE the row that was copied) because these will be variables that the user edits to adjust A3:G3 before it gets copied.
To help, here is a script that #Cooper built for me to push me in the right direction:
function onEdit(e){
var sh=e.range.getSheet();
if (sh.getName()=='Sheet1' && e.range.columnStart==8 && e.range.rowStart>1 && e.value=="submitResponse") {
var msh=e.source.getSheetByName("Master");
msh.appendRow(sh.getRange(e.range.rowStart,1,1,7).getDisplayValues()[0]);
}
}
from this previous question: Modifying a flexible Google Sheets onEdit script to accommodate a wider range of inputs
Update:
function onEdit(e){
var done = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange('Y:Y');
var sh=e.range.getSheet();
if (sh.getName()=='submit' && e.range.columnStart==25 && e.range.rowStart>1 && e.value=="submit") {
var msh=e.source.getSheetByName("db");
msh.appendRow(sh.getRange(e.range.rowStart,1,1,24).getDisplayValues()[0]);
done.clearContent();
}
}
Update 2:
function onEdit(e){
var submitColumn = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange('Y:Y');
var rowAbove = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange('J11:L11');
var sh=e.range.getSheet();
if (sh.getName()=='submit' && e.range.columnStart==25 && e.range.rowStart>1 && e.value=="submit") {
var msh=e.source.getSheetByName("db");
msh.appendRow(sh.getRange(e.range.rowStart,1,1,24).getDisplayValues()[0]);
submitColumn.clearContent();
rowAbove.ClearContent();
}
}
Solution
With Update 2 you were really close to solve this issue. However, you had a typo, replace:
rowAbove.ClearContent();
with
rowAbove.clearContent();
I hope this has helped you. Let me know if you need anything else or if you did not understood something. :)

Using an If statement to check if a checkbox in a column of checkboxes is checked, and then run the remaining script

I cannot get the below to work. Any help would be appreciated. I posted a similar post which I had some great assistance with, however when I apply the same concepts here I cannot get it to work. Thank you.
/** #OnlyCurrentDoc */
function onEdit(e) {
//This IF statement ensures that this onEdit macro only runs when cells A1:A2 are edited
if (
e.source.getSheetName() == "Finances 2020" &&
e.range.getColumn() == 1 &&
1<=e.range.getRow()<=2
) {
//Cells A1:A2 are checkboxes. This section ensures the following script only runs when the checkbox is checked (and not when unchecked).
var checkboxtest = e.range.getValue()
if (checkboxtest == true) {
//Some script to test if the above works (by grabbing some text from a cell near by and pasting it into another):
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var test = spreadsheet.getRange(3, 3).getValues();
spreadsheet.getRange('A3').setValues(test);
}
}
}
;
You haven't defined a sheet for getRange(3, 3), so your code is silently failing. Try running just that portion of your script and you'll see what I mean.
function test() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var test = spreadsheet.getRange(3, 3).getValues();
Logger.log(test); // Error: Cannot find method getRange(number,number).
}
You can easily resolve this by defining a sheet. I'm not sure if you want to use the active sheet or not, but I'll assume you do. So your code could look like this, and it works.
/** #OnlyCurrentDoc */
function onEdit(e) {
//This IF statement ensures that this onEdit macro only runs when cells A1:A2 are edited
if (
e.source.getSheetName() == "Finances 2020" &&
e.range.getColumn() == 1 &&
1<=e.range.getRow()<=2
) {
//Cells A1:A2 are checkboxes. This section ensures the following script only runs when the checkbox is checked (and not when unchecked).
var checkboxtest = e.range.getValue()
if (checkboxtest == true) {
//Some script to test if the above works (by grabbing some text from a cell near by and pasting it into another):
var sheet = SpreadsheetApp.getActiveSheet();
var test = sheet.getRange(3, 3).getValues();
sheet.getRange('A3').setValues(test);
}
}
}

Codes doesn't work in formulated cell (ex. vlookup)

Anyone who can help me with this?
Why my codes wont work in formulated cell?
I want to run my codes in formulated cell like vlookup
Is there a way to solve this problem?
function onEdit(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getSheets()[0]; // 0 == first sheet.
var ee = ss.getActiveRange().getA1Notation();
if (ee == "A1") { // Check if edited cell is the one we're watching.
if (e.value == "DONE"){ // If the value == "DONE", do stuff.
var toggle = sh.getRange("A1:M1");
for(var i=0;i<50;i++) {
if( i%2 == 0 )
toggle.setBackground("GOLD");
else
toggle.setBackground("WHITE");
SpreadsheetApp.flush();
Utilities.sleep(500);
}
}
}
}
On edit and on change triggers only are fired by changes directly made by the user, not when the result of a formula changes or a change is made by code.
Use conditional formatting instead.