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
Related
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:
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
Here is a video of what I am trying to do.
[https://soapbox.wistia.com/videos/rVDNWFKiuW][1]
The YT video I am trying to copy is
https://youtu.be/YBd7Bv41A1Q
Here is the script he is using.
Script:
function onEdit(e) {
const src = e.source.getActiveSheet();
const r = e.range;
if (r.columnStart != 3 || r.rowStart == 1 || e.value == src.getName()) return;
const dest = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(e.value);
src.getRange(r.rowStart,1,1,3).moveTo(dest.getRange(dest.getLastRow()+1,1,1,3));
src.deleteRow(r.rowStart);
}
It works like this just using a simple trigger.
NO Need to create an installable trigger. I put a dataValidation in Sheet0 column 3 from a list Sheet1,Sheet2 and it works just fine.
function onEdit(e) {
const sh = e.range.getSheet();
if (e.range.columnStart != 3 || e.range.rowStart == 1 || e.value == sh.getName()) return;
const dest = e.source.getSheetByName(e.value);
sh.getRange(e.range.rowStart,1,1,3).moveTo(dest.getRange(dest.getLastRow()+1,1,1,3));
sh.deleteRow(e.range.rowStart);
}
Demo:
You need to create a Trigger to allow the script to perform the changes once you make a change on the drop down menu.
Example:
In this image I used the same script shown in the video and I added it as a trigger for the script. Note that it was added as an OnEdit type of trigger:
I have an onEdit function for my Google Sheet where when a checkbox in column X of the source sheet is checked, it will copy the values of the cells in column Y and Z, then paste them into column A and B of the destination sheet. It worked fine previously but it suddenly stopped working and I can't seem to find the problem. I've tried adding a trigger for it to my Script Editor project as well, but nothing happens when I check the checkbox, and there is no error message. I'm quite new to this so any help is much appreciated.
Here's the code that I've been using:
function onEdit(e) {
const src = e.source.getActiveSheet();
const r = e.range;
if(src.getName() !="Source" || r.columnStart != 24 || r.rowStart == 1,2) return;
const dest = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Destination");
src.getRange(r.rowStart,25,1,2).copyTo((dest.getRange(dest.getLastRow()+1,1,1,2)),SpreadsheetApp.CopyPasteType.PASTE_VALUES,false);
}
I believe your goal is as follows.
When the ckeckbox of the column "X" is checked, you want to copy the values of columns "Y" and "Z" from "Source" sheet to "Destination" sheet.
Although, unfortunately, I cannot understand the script of It worked fine previously, when your showing script is modified, how about the following modification?
Modified script:
function onEdit(e) {
const src = e.source.getActiveSheet();
const r = e.range;
if (src.getName() != "Source" || r.columnStart != 24 || r.rowStart <= 2 || !r.isChecked()) return;
const dest = e.source.getSheetByName("Destination");
src.getRange(r.rowStart, 25, 1, 2).copyTo((dest.getRange(dest.getLastRow() + 1, 1, 1, 2)), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
}
I thought that you wanted to do r.rowStart == 1,2 as r.rowStart <= 2.
In order to check the checkbox, isChecked() is used.
Reference:
isChecked()
I cannot get this code to do what I am trying to get it to do. If there is a TRUE in Column 10 then run the Sort Query on SendToTotalSales - NOT ONLINERELOCATION
function MoveDonations(e) {
var sh=e.range.getSheet();
if(sh.getName()!='ONLINERELOCATION')return
if(e.range.columnStart==10 && e.value=="TRUE") {
e.source.getActiveSheet().getRange(row,????).setFormula('=SORT(QUERY(ONLINERELOCATION!A2:J,"SELECT A,
F, G, D",0))');
}
//var tsh=e.source.getSheetByName("SendToTotalSales");
//var trg=tsh.getRange(tsh.getLastRow()+1,1);
//sh.getRange(e.range.rowStart,1,1,4).copyTo(trg);
}
I am hoping I was Close !
Thanks in Advance
When the checkbox of the column "J" in the sheet of ONLINERELOCATION is checked, you want to copy the values of the columns "A,D,F,G" to the sheet of SendToTotalSales as "A,F,G,D".
You want to achieve this using Google Apps Script.
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
Sample script:
In this case, you can also use the simple OnEdit event trigger (onEdit(e)).
function MoveDonations(e) {
var range = e.range;
var sheet = range.getSheet();
if (sheet.getSheetName() == "ONLINERELOCATION" && range.columnStart == 10 && range.columnEnd == 10 && range.rowStart >= 2 && e.value == "TRUE") {
var [[a,,,d,,f,g]] = sheet.getRange(range.rowStart, 1, 1, 7).getValues();
e.source.getSheetByName("SendToTotalSales").appendRow([a, f, g, d]);
}
}
In order to run the script, please check the checkbox of the column "J" in the sheet of ONLINERELOCATION. By this, the values of the row are copied to SendToTotalSales.
Note:
I think that when '=SORT(QUERY(ONLINERELOCATION!A2:J,"SELECT A, F, G, D",0))' is used, all values are put. So I proposed above script.
And I'm not sure whether the duplicate process is required for your situation.