I am trying to get this script working where it will work when I put a "yes" or "no" for a choice it will move based on the "yes" to the targeted sheet "Completed Dispatch"
The original sheet is called "Tracker" and the column that I need it to check the "Yes or No" is called "Dispatched"
It doesn't work and I am not that great in GAS
The actual problem I am having is when I make the decision from no to Yes it does not move the row and does not do anything at all.
function onEdit(e) {
var ss = e.source,
sheet = ss.getActiveSheet(),
range = e.range,
targetSheet,
columnNumberToWatch = 9; // column A = 1, B = 2, etc
if (sheet.getName() === "Tracker" && e.value === "Dispatched" && e.range.columnStart === columnNumberToWatch) {
targetSheet = "Completed Dispatch"
} else if (sheet.getName() === "Completed Dispatch" && e.value === "Incomplete" && e.range.columnStart === columnNumberToWatch) {
targetSheet = "Tracker"
}
ss.getSheetByName(targetSheet)
.appendRow(sheet.getRange(e.range.rowStart, 1, 1, sheet.getLastColumn())
.getValues()[0])
sheet.deleteRow(e.range.rowStart);
}
Related
I am having 2 google sheet, having checkbox on 1st Sheet.
Requirement: When the checkbox is checked , Value from Cell C5 need to be stored in the Sheet2. When the checkbox is checked i.e value is 'True', the value in cell C5 of the Sheet1 needs to be submitted in the Sheet2 in the continuous manner.
I know this is the silly requirement, but I failed after several attempts as well.
Try
function onEdit(event) {
var sh1 = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if (sh1.getName() == 'Sheet1' && r.getA1Notation() == 'D5' && r.getValue() == true) {
var sh2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet2')
sh2.getRange('A' + (sh2.getLastRow() + 1)).setValue(r.offset(0, -1).getValue())
r.setValue(!r.getValue())
}
}
Store C5 in Sheet2 When Checkbox is checked
function onEdit(e) {
var sh1 = e.range.getSheet();
if (sh1.getName() == 'Sheet1' && e.range.columnStart == 4 && e.range.rowStart == 5 && e.value == "TRUE") {
var sh2 = e.source.getSheetByName('Sheet2');
sh2.getRange(sh2.getLastRow() + 1, 1).setValue(e.range.offset(0, -1).getValue());
e.range.setValue("FALSE");
}
}
I am trying to create radio checkboxes, i managed to make it work when the checkboxes are in vertical as shown in the exemple sheet bellow with the tab name 'Radio 1' but i have another tab 'Radio 2' where the checkboxes are aligned horizontally and are not aligned one after the other but after every 2 cells, the first Radio works with the following code
function onEdit(e) {
if (e.range.columnStart != 2 || e.range.rowStart == 1 || e.range.rowStart > 8 || e.value != "TRUE") return;
let r = SpreadsheetApp.getActive().getActiveSheet().getRange(2,2,7);
let checks = r.getValues();
for (let i in checks){
if(checks[i][0] == true && +i != e.range.rowStart - 2)
checks[i][0] = false;
}
r.setValues(checks);
}
but i cant make it to work when adapting to the second radio tab, any ideas?
sheet link
Try
function onEdit(e) {
var sh = e.source.getActiveSheet()
if (sh.getName() == 'Radio 1') {
if (e.range.columnStart != 2 || e.range.rowStart == 1 || e.range.rowStart > 8 || e.value != "TRUE") return;
let r = SpreadsheetApp.getActive().getActiveSheet().getRange(2, 2, 7);
let checks = r.getValues();
for (let i in checks) {
if (checks[i][0] == true && +i != e.range.rowStart - 2)
checks[i][0] = false;
}
r.setValues(checks);
}
else if (sh.getName() == 'Radio 2') {
var cel = e.source.getActiveRange()
if (cel.getRow() == 11 && cel.getValue() == true) {
var r = sh.getRange('H' + cel.getRow() + ':S' + cel.getRow())
var checks = r.getValues()
for (var i=0;i<checks[0].length;i+=2) {
if (checks[0][i] == true && i != cel.getColumn()-8)
if (checks[0][i]==true) {checks[0][i] = false};
}
r.setValues(checks);
}
}
}
If you want to apply to multi-rows, change this cel.getRow() == 11
In your situation, how about the following modification?
Modified script:
In this modification, the cell ranges of checkboxes are used.
function onEdit(e) {
// This is from your sample Spreadsheet.
// The cell ranges of checkboxes are set as A1Notation.
const obj = {
'Radio 1': ["B2", "B3", "B4", "B5", "B6", "B7", "B8"],
'Radio 2': ["H11", "J11", "L11", "N11", "P11", "R11"]
};
const sheet = e.source.getActiveSheet();
const sheetName = sheet.getSheetName();
const a1Notation = e.range.getA1Notation();
if (!obj[sheetName] || !obj[sheetName].includes(a1Notation) || e.range.getValue() === false) return;
sheet.getRangeList(obj[sheetName].filter(e => e != a1Notation)).uncheck();
}
When this script is run, when a checkbox is checked on "Radio 1" or "Radio 2", other checked checkboxes are unchecked.
If you want to add "Radio 3" and others, please add them to obj.
Note:
When you changed the checkboxes and the sheet names, please modify obj. Please be careful about this.
References:
filter()
uncheck() of Class RangeList
I am trying to combine two onEdit functions in a single App Script. I have read through other posts on this subject and feel like I have integrated their suggestions but it is still not working.
Function A (timeStamp) places a time stamp in column B when columnn A is edited
Function B (arhviveRow) copies the edited row to sheet "Inactive" when a check box is marked True on sheet "Active" and then deletes the row from sheet "Active".
Both functions work correctly when run separately, but only the second function works when combined.
function onEdit(e) {
timeStamp();
archiveRow();
//Automatically stamp date and time on line
function timeStamp() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Active" ) { //checks that we're on Sheet1 or not
var r = s.getActiveCell();
if( r.getColumn() == 1 ) { //checks that the cell being edited is in column A
var nextCell = r.offset(0, 1);
if( nextCell.getValue() === "" ) //checks if the adjacent cell is empty or not?
nextCell.setValue(new Date() + new (Time));
}
}
}
//Archive and delete line
function archiveRow(){
//ColumnNumberToWatch defines which column on sheet to monitor for an edit
var ss = e.source,
sheet = ss.getActiveSheet(),
range = e.range,
targetSheet,
columnNumberToWatch = 9; // column A = 1, B = 2, etc
// "sheet.getName() ===" defines which sheet to watch for the edit
// "targetSheet =" defines sheet to copy data to
if (sheet.getName() === "Active" && e.value === "TRUE" && e.range.columnStart === columnNumberToWatch) {
targetSheet = "Inactive"
} else if (sheet.getName() === "Inactive" && e.value === "FALSE" && e.range.columnStart === columnNumberToWatch) {
targetSheet = "Active"
}
//Copies data to last row in targetSheet and then deletes from source sheet
ss.getSheetByName(targetSheet)
.appendRow(sheet.getRange(e.range.rowStart, 1, 1, sheet.getLastColumn())
.getValues()[0])
sheet.deleteRow(e.range.rowStart);
}
}
Any insights into where I went wrong would be greatly appreciated.
Thanks!
The new Date() object should already contain the time, and there is no Javascript Object for new Date(Time).
It should look like this:
function timeStamp() {
var s = SpreadsheetApp.getActiveSheet();
Logger.log(s);
if( s.getName() == "Active" ) { //checks that we're on Sheet1 or not
var r = s.getActiveCell();
if( r.getColumn() == 1 ) { //checks that the cell being edited is in column A
var nextCell = r.offset(0, 1);
if( nextCell.getValue() === "" ) {//checks if the adjacent cell is empty or not?
nextCell.setValue(new Date().toLocaleString());
}
}
}
}
Sample output for the date:
Active Sheet
Once the checkbox was clicked setting the value to TRUE, appends the row to the "Inactive" sheet.
Inactive Sheet
function onEdit(e) {
const sh = e.range.getSheet();
const name = sh.getName():
if (name == "Active" && e.range.columnStart == 1) {
e.range.offset(0, 1).setValue(Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "MM/dd/yyyy HH:mm:ss"));
}
if (name == 'Active' && e.range.columnStart == 9 && e.value == "TRUE") {
let tsh = e.source.getSheetByName('Inactive');
tsh.appendRow(sh.getRange(e.range.rowStart, 1, 1, sh.getLastColumn()).getValues()[0]);
sh.deleteRow(e.range.rowStart())
} else if (name = 'Inactive' && e.range.columnStart == 9 && e.value == "FALSE") {
let tsh = e.source.getSheetByName('Active');
tsh.appendRow(sh.getRange(e.range.rowStart, 1, 1, sh.getLastColumn()).getValues()[0]);
sh.deleteRow(e.range.rowStart());
}
}
my current sheet checks 'Dec 2019' sheet and if the criteria is met. He will then make a copy of the row to sheet2.
Right now, I wanted the search not just on 'Dec 2019' but also in other monthly sheet. I have different worksheet for each month.
Need help on making my script correct.
function onEdit(event) {
// assumes source data in sheet named main
// target sheet of move to named Completed
// getColumn with drop-downs is currently set to column 3 or C
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Dec 2019" && r.getColumn() == 7 && r.getValue() == "No") {
var row = r.getRow();
var targetSheet = ss.getSheetByName("Sheet2");
var values = s.getRange(row, 1, 1, 10).getValues()[0];
var resultRow = [values[0], values[1], values[2]];
targetSheet.appendRow(resultRow);
}
}
Your current code contains
if(s.getName() == "Dec 2019" && r.getColumn() == 7 && r.getValue() == "No") {
This means that your request will carried out if the name of the edited sheet is "Dec 2019" and the edited column "7" and the value "No"
If you want to apply the same condition (edited column "7" and the value "No") also to other sheets - either you need to implement an or operator "||",
e.g.
if((s.getName() == "Dec 2019" || s.getName() == "Nov 2019") && r.getColumn() == 7 && r.getValue() == "No") {
or, if you want to perform the request for all sheets in your spreadsheet - just remove the frist condition completely:
if(r.getColumn() == 7 && r.getValue() == "No") {
I am trying to get the date to populate in a column AC (29) of the google sheet, when I enter deployed in Column Y (25). Here is the script I've currently created, and can't figure out why it's not working.
function onEdit(e) {
var colToWatch = 25, colToStamp = 29;
var valueToWatch = "Deployed";
if (e.range.columnStart === colToWatch && (e.value === valueToWatch || typeof e.value == 'object'))
e.source.getActiveSheet()
.getRange(e.range.rowStart, colToStamp)
.setValue(typeof e.value === 'object' ? null : new Date());
}
columnStart() and rowStart() are not methods available to the Range object in Google Apps Script. You need to use getColumn() and getRow() instead. Checking the value type multiple times seems unnecessary given that you're already using ===, and it's not listed in your problem description, so I'm removing it. Moreover, the typeof value is necessarily going to be a string–it will never be an object.
function onEdit(e) {
var colToWatch = 25, colToStamp = 29;
var valueToWatch = "Deployed";
if (e.range.getColumn() === colToWatch && e.value === valueToWatch) {
e.range.getSheet().getRange(e.range.getRow(), colToStamp).setValue(new Date());
}
}