Dropdown box edit to trigger setValue - google-apps-script

I'm looking for a way to use a dropdown(B5) trigger, just any edit, to copy the value of a cell(L6) to another(E3). I can't put the formula directly in the cell as I need to be able to edit it.
I've really hit a wall, the help would be appreciated!

Try using triggers in your script, this can be done by having specific function names, like onEdit(e).
Sample Code:
function onEdit(e) {
const sheet = e.range.getSheet();
if (e.range.getA1Notation() == "B5") {
sheet.getRange("E3").setValue(sheet.getRange("L6").getValue());
}
}
Note:
When you put this code in the script editor, if you run this manually (perhaps to authenticate), it will return an TypeError error. The script will run properly every time cell B5 is edited.
Sample Output:

function onEdit(e) {
const sh = e.range.getSheet();
if (sh.getName() == 'Enter Sheet Name' && e.range.columnStart == 2 && e.range.rowStart == 5) {
sh.getRange(3,5).setValue(sh.getRange(5,2));
}
}

Related

How to delete a row as indicated in a cell in google app script?

What I want to do:
I write a row number in a cell, let's say F1, and the value is 9.
I want to add a button that calls a script that will delete the line as I wrote it into the cell, so in this example, line 9.
So I start with:
var s = SpreadsheetApp.getActive()
var sheet = s.getSheetByName('Books');
var row = sheet.getRange('F1');
However this:
sheet.deleteRow(row.getValue());
will give an out of bounds error. getDisplayValue does not work as well and putting it into an parseint doesn't work either.
The 9 hardcoded
sheet.deleteRow(9);
works of course.
I also debbuged and did a
sheet.getRange('G1').setValue(row.getValue());
to verify that the 9 is indeed there, which it is, the 9 will be copied to cell G1.
sheet.getRange('A'+row.getValue()+':J'+row.getValue()).clearContent();
meanwhile will have it treat it as a A:J and delete every line not just one. Doing toString after getValue did also not change anything.
What am I missing here to turn the cell value into a range?
Sheet.deleteRow(value of F1):
function onEdit(e) {
const sh = e.range.getSheet();
if(sh.getName() == "Your sheet name" && e.range.columnStart == 6 && e.range.rowStart == 1 && e.value) {
sh.deleteRow(e.value)
}
}
Cannot call this function from script editor must save in project and edit F1 of Your Sheet. You also need to edit line 3 and add the name of the appropriate sheet.
Using a checkbox for a button:
function onEdit(e) {
e.source.toast("Entry")
const sh = e.range.getSheet();
if(sh.getName() == "Sheet0" && e.range.columnStart == 1 && e.range.rowStart == 1 && e.value == "TRUE") {
e.source.toast("Gate1")
e.range.setValue("FALSE")
sh.deleteRow(sh.getRange("F1").getValue());
}
}
Demo:

Copying Data from one Sheet to another using Check Boxes

I am trying to figure out a way to "automate" a task in google sheets. I have a data model that lists all the data on one sheet of all the 'hit percentages' of sports props. The data changes as games complete. I'd like to have check boxes(or something similar) next to the rows that when it's checked it copies that row and then adds it into a sheet called 'Logs" after the last row.
I put this sheet together as an example:
https://docs.google.com/spreadsheets/d/1KocfnKXK1sPmBr3uQfcn8dC6MYV0JUWDz5Ql3AgTMEI/edit?usp=sharing
I believe your goal is as follows.
When you checked the checkbox of column "B" of "Changing Data" sheet, you want to copy the row to the next row of the last row of "Log" sheet.
In this case, how about the following modification? In this modification, in order to check whether the checkbox is checked, the simple trigger of OnEdit is used.
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 check the checkbox of column "B" of "Changing Data" sheet. By this, the script is automatically run by the OnEdit trigger.
function onEdit(e) {
const srcSheetName = "Changing Data"; // This is from your sample Spreadsheet.
const dstSheetName = "Log"; // This is from your sample Spreadsheet.
const { range, source } = e;
const sheet = range.getSheet();
if (sheet.getSheetName() != srcSheetName || range.columnStart != 2 || !range.isChecked()) return;
const dstSheet = source.getSheetByName(dstSheetName);
range.offset(0, -1, 1, 2).copyTo(dstSheet.getRange(dstSheet.getLastRow() + 1, 1));
}
Reference:
Simple Triggers
Copy Row To Log Sheet
function onEdit(e) {
//e.source.toast("Entry");
const sh = e.range.getSheet();
if(sh.getName() == "Sheet0" && e.range.columnStart == 7 && e.range.rowStart > 1 && e.value == "TRUE") {
//e.source.toast("Gate1");
e.source.toast("Gate1");
const lsh = e.source.getSheetByName("Log");
const vs = sh.getRange(e.range.rowStart,1,1,sh.getLastColumn()).getValues();
lsh.getRange(lsh.getLastRow() + 1, 1,vs.length,vs[0].length).setValues(vs);
}
}
Sheet0:
Alternative Solution
You can try this another implementation using appendRow() & getValue() methods.
This sample will only run the onEdit() function when column B check-boxes are selected on the Changing Data sheet. The function will only copy the text on Column A row that is adjacent to the selected checkbox.
Script
function onEdit(e) {
e.source.getActiveSheet().getName() == 'Changing Data' && e.range.getColumn() == 2 && e.value == "TRUE" ? e.source.getSheetByName('Log').appendRow([e.source.getSheetByName('Changing Data').getRange('A' + e.range.getRow()).getValue()]) : _ ;
}
Alternate Script if you want to include check-boxes to be also copied:
function onEdit(e) {
e.source.getActiveSheet().getName() == 'Changing Data' && e.range.getColumn() == 2 && e.value == "TRUE" ?
(e.source.getSheetByName('Log').appendRow([e.source.getSheetByName('Changing Data').getRange('A' + e.range.getRow()).getValue()]) ?
(e.source.getSheetByName('Changing Data').getRange('B' + e.range.getRow()).copyTo(e.source.getSheetByName('Log').getRange('B' + e.source.getSheetByName('Log').getLastRow()))) : _) : _;
}
Demo
Changing Data sheet
Logs sheet

How to select a cell in one column in the same row that the other cell, that is being edited in the other column using Google Apps Script?

I've been wondering if there was a way to select a cell in a particular column, when the other particular column has been edited, using Google Apps Script?
For example: **If someone edits B3, my script should select C3 (if someone edits B23, then C23), but someone edits let's say A5- then nothing happens.**
See the example picture below
Thank you in advance!
It's possible through the onEdit trigger, data from the e/event object, and .activate().
Try:
function onEdit(e) {
if (e.source.getActiveSheet().getName() === `Sheet1`) {
if (e.range.rowStart === 5 && e.range.columnStart === 1) return
e.source.getActiveSheet()
.getRange(e.range.rowStart, e.range.columnStart+1)
.activate()
}
}
Commented:
function onEdit(e) {
// If the sheet ('Sheet1') was edited..
if (e.source.getActiveSheet().getName() === `Sheet1`) {
// "Cancel" if A5 was selected.
if (e.range.rowStart === 5 && e.range.columnStart === 1) return
// Get the active sheet
e.source.getActiveSheet()
// Select the range of "edited row", "edited column +1"
.getRange(e.range.rowStart, e.range.columnStart+1)
// "Select" the cell.
.activate()
}
}
If you would like to 'ignore' more than A5, please let me know and I can update the examples for you.
Learn More:
onEdit Event Object
Range.activate()
Try this
function onEdit(e) {
const sh = e.range.getSheet();
if (sh.getName() == `YOUR SHEET NAME` && e.range.columnStart == 2 ) {
e.range.offset(0,1).activate();
}
}
activate

Google Spreadsheet Script - Increasing value of a cell in sheet2 before clearing cell in sheet1

Completely new when it comes to scripting etc but managed to get some small scripts working via looking up things here and there, so please bear with me.
I am looking to create a script that allows me to enter a value in a cell in sheet1 that adds the value to a cell in sheet2 (so not a simply copy over). Once the value in sheet1 has been entered, it clears itself.
Sadly, I have no script to show you of what I have tried, as this has me completely stumped. If anyone has any pointers or can provide help, it would be much appreciated.
Edit:
Based on below code, I've tried something like this:
function onEdit(e) {
//e.source.toast('Entry');//used for debugging
//Logger.log(JSON.stringify(e));//get event object
const sh = e.range.getSheet();
if(sh.getName() == "Dashboard" && e.getRange("C17") && e.value) {
let rg = e.source.getSheetByName("Data").getRange("D2")
rg.setValue(Number(rg.getValue()) + Number(e.value));
e.range.setValue('');
}
}
Though not getting it to work.
Add B2 Sheet0 to B2 Sheet1
function onEdit(e) {
//e.source.toast('Entry');//used for debugging
//Logger.log(JSON.stringify(e));//get event object
const sh = e.range.getSheet();
if(sh.getName() == "Sheet1" && e.range.columnStart == 2 && e.range.rowStart == 2 && e.value) {
let rg = e.source.getSheetByName("Sheet2").getRange(e.range.rowStart,e.range.columnStart)
rg.setValue(Number(rg.getValue()) + Number(e.value));
e.range.setValue('');
}
}
Note: you cannot run this type of function from the script editor just paste into script editor, save and go to Sheet1 B2 and type in a number and hit return. Then look on Sheet2 to see B2 change.
onEdit trigger

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);
}
}
}