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.
Related
I'm new to Google App Script and trying to create a queue-like spreadsheet.
The current design is that: in sheet A, there's a column of checkboxs, and if any one of them is checked, the entire row would be moved to the end of another sheet, say sheet B.
The way I calculate the end of the sheet B is as followed:
(I read from a different stackoverflow post)
var Avals = dest.getRange("B:B").getValues();
var Alast = Avals.filter(String).length;
var lastRow = String(Alast + 1);
The issue I'm having here is that when I check two checkboxes quickly one by one, they are mapped to the same lastRow in sheet B, because the second checkbox triggers the onEdit trigger before the first one has moved the entire row to the bottom of the sheet B.
Does anyone know how to solve this issue?
Right now, the workaround is since it's a shared google sheet, I asked my colleagues not to click the checkbox column at the same time.
This seems to be working for what little testing I've done
function onEdit(e) {
LockService.getScriptLock().tryLock(2000)
const sh = e.range.getSheet();
if(sh.getName() == "Sheet0" && e.range.columnStart == 1 && e.range.rowStart > 1 && e.value == "TRUE") {
let tsh = e.source.getSheetByName("Sheet1");
let vs = sh.getRange(e.range.rowStart, 1, 1,sh.getLastColumn()).getValues();
tsh.getRange(tsh.getLastRow() + 1, 1 , vs.length, vs[0].length).setValues(vs);
}
LockService.getScriptLock().releaseLock();
}
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
I have script for Google Sheets that I collected on interwebs and got some help here. No I have 2 onEdit in conflict. I overcome that by creating script Trigger for onEdit2. It works but I don't think it is the best solution. Could you help get those two separated onEdit with if functions into one, please?
//Dependent Dropdown list
function onEdit(e){ // Function that runs when we edit a value in the table.
masterSelector(master1,master2,master3,master4);
var activeCell = e.range; // It returns the coordinate of the cell that we just edited.
var val = activeCell.getValue(); // Returns the value entered in the column we just edited.
var r = activeCell.getRow(); // returns the row number of the cell we edit.
var c = activeCell.getColumn(); // returns the column number of the cell we edit.
var wsName = activeCell.getSheet().getName();
if (wsName === masterWsName && c === firstLevelColumn && r > masterNumberOfHeaderRows) { // the if delimits the section sensitive to modification and action of the onEdit.
applyFirstLevelValidation(val,r);
} else if (wsName === masterWsName && c === secondLevelColumn && r > masterNumberOfHeaderRows){
applySecondLevelValidation(val,r);
}
} // end of onEdit
// addRow by checkboxes
function onEdit2(e) {
masterSelector(master1,master2,master3,master4);
//IF the cell that was edited was in column 4 = D and therefore a checkbox AND if the cell edited was checked (not unchecked):
if (e.range.columnStart === 4 && e.range.getValue() === true) {
var sheet = SpreadsheetApp.getActiveSheet(),
row = sheet.getActiveCell()
.getRow(),
//(active row, from column, numRows, numColumns)
rangeToCopy = sheet.getRange(row, 1, 1, 30);
sheet.insertRowAfter(row);
rangeToCopy.copyTo(sheet.getRange(row + 1, 1));
//Reset checked boxes in column 4
sheet.getRange(row,4,2,1).setValue(false);
}
}
Whole script is here, if needed.
A script cannot contain two functions with the same name. Rename your first onEdit function to onEdit1 (actually it will be better to assign a descriptive name) and the second function as onEdit2, then put them both in one function named onEdit and pass the parameter e to both of them:
function onEdit(e){
onEdit1(e);
onEdit2(e);
}
Related:
Two OnEdit functions not working together
Best Practices for Multiple OnEdit Functions
How to run multiple onEdit functions in the same google script (google sheets)?
Bracketing multiple onEdit functions
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 am from a non-computer background trying to execute a small code and am sharing some inputs which I think are relevant to this query (based on my evening of research). Can please someone help me create this code to execute the "purpose"?
Purpose- Use a checkbox to hide some columns which are not in order or next to one another.
Range in question: A:S
Checkbox location: Cell- "K47"
If above is true, Hide columns- A, B, D, F, G, H, I, L, M, P, Q, R, and S
If above is false, Show all columns A to S
Sheet name- "USER"
Regards,
Kavita
Thanks for the inputs guys. I had a friend help me and here's what I got.
Note, ["ABHISHEK", "MITESH", "KAVITA", "MEENA","SUNIL"] are the sheet names where i wanted the switch to work . the switch location has been changed from K47 to E1
// #ts-nocheck
function onEdit()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
["ABHISHEK", "MITESH", "KAVITA", "MEENA","SUNIL"].forEach(function (s) { var sheet = ss.getSheetByName(s);
var st =sheet.getRange("E1").getValue();
if(st == true)
{
sheet.hideColumn(sheet.getRange("A:B"));
sheet.hideColumn(sheet.getRange("D:D"));
sheet.hideColumn(sheet.getRange("F:I"));
sheet.hideColumn(sheet.getRange("L:M"));
sheet.hideColumn(sheet.getRange("P:S"));
}
else
{
sheet.unhideColumn(sheet.getRange("A:B"));
sheet.unhideColumn(sheet.getRange("D:D"));
sheet.unhideColumn(sheet.getRange("F:I"));
sheet.unhideColumn(sheet.getRange("L:M"));
sheet.unhideColumn(sheet.getRange("P:S"));
}
}
)
}
In the Script Editor, write a function (a simple trigger) for onEdit(). In your code, evaluate if the checkbox was checked using sheet.getRange("K47") and range.getValue() (the sheet can be obtained using SpreadsheetApp.getActive().getSheetByName('USER')). Contain within an if-else statement the actions you listed and then run the script once to authorize. For resources on how to further translate your pseudocode into Apps Script, I would suggest https://www.w3schools.com/js/ and https://developers.google.com/apps-script/guides/sheets.
User86530 points you to the correct direction. But I would prefer using the event object.
Code:
function onEdit(e) {
var spreadsheet = e.source;
var range = e.range;
var value = e.value;
// if sheet name is 'USER' and edited cell is 'K47'
if(spreadsheet.getSheetName() == 'USER' && range.getRow() == 47 && range.getColumn() == 11) {
sheet = spreadsheet.getSheetByName('USER');
if(value == "TRUE") {
// Hide columns
sheet.hideColumns(1, 2); // A-B
sheet.hideColumns(4, 1); // D
sheet.hideColumns(6, 4); // F-I
sheet.hideColumns(12, 2); // L-M
sheet.hideColumns(16, 4); // P-S
}
else {
sheet.showColumns(1, 19); // A-S
}
}
}
Output:
Note:
Add the code above as script for the sheet, save and try to edit the cell right away.
This will only get triggered if K47 is edited on USER
Resource:
Event