Google Script to Copy cells value to another Google sheet - google-apps-script

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

Related

Google Sheets - OnEdit - Separate Range

I am using the following code to automatically turn inputs in columns 1 and 4 of a sheet into a hyperlink. Whenever any range beyond 1 cell is pasted into the sheet only the first value will be hyperlinked into all cells in the range. Is there a way to get the cells within the range so I can have it check each cell for their individual value and column?
Thanks for any help!
function onEdit(e) {
// Set a comment on the edited cell to indicate when it was changed.
let range = e.range;
let order = range.getValue()
if (!( order === ''))
{
if (range.getColumn() == 1 || range.getColumn() == 4) {range.setValue("=hyperlink(\""+order+"\", \""+order+"\")");}
}
}
If you wish to tie it down to a single sheet
function onEdit(e) {
//e.source.toast('Entry')
//Logger.log(JSON.stringify(e));
const sh = e.range.getSheet();
if (sh.getName() == "Sheet1" && e.range.rowStart > 1 && (e.range.columnStart == 1 || e.range.columnStart == 4) && e.value) {
//e.source.toast("flag1");
e.range.setValue(`=hyperlink("${e.value}","${e.value}")`);
}
}

Combining two onEdit() in single Google Sheets App Script

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

Copy value of cell (A) to another cell (B) based on cell (C)

I need to write a Google Apps Script that states the following:
When Column 4 is "Action", then copy the value in Column 2 to Column 9. So far I have this:
function autoAssign() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
var r = s.getActiveRange();
var c = s.getActiveCell()
var row = r.getRow();
if(s.getName() == "New Requests" && r.getColumn() == 4 && r.getValue() == "Action") {
s.getRange(row,2).copyTo(ss.getRange(row,9)) ;
}
}

conditional formatting with checkboxes

Is there a way to format a checkbox so that when it is "checked", The data in the column beside it will be moved to another column?
function onEdit(e) {
const sh = e.range.getSheet();
if (sh.getName() == 'Sheet1' && e.range.columnStart == 1 && e.value == 'TRUE') {
e.range.offset(0, 5).setValue(e.range.offset(0, 1).getValue());
e.range.offset(0, 1).setValue('');
e.range.setValue('FALSE');
}
}
As player0 mentioned, for this to work, you need Google Apps Script's Spreadsheet Service with the help of Event Objects.
The code below should work on the sample data below.
Sample data:
Code:
function onEdit(e) {
var sheet = e.source.getActiveSheet();
var range = e.range;
var column = range.getColumn();
// if edited data is in Sheet1 Column A and having value TRUE (checkbox ticked)
if (sheet.getName() == "Sheet1" && column == 1 && e.value == "TRUE") {
// get 1 column to the right of checkbox and assign to dataValue
var dataRange = range.offset(0, 1);
var dataValue = dataRange.getValue();
// get 3 columns to the right of dataValue and assign to transferToRange
var transferToRange = dataRange.offset(0, 3);
// set dataValue as value to transferToRange
transferToRange.setValue(dataValue);
// remove data from dataRange after
dataRange.setValue("");
}
}
Output:
Note:
Above code is just one of the many possible solutions you can create via Apps Script.
Read Apps Script for more information.
References:
Spreadsheet Service
Event Objects

Google Sheets, Moving from one Tab to another via input

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