Google Script - creating an archive if one row says "closed" - google-apps-script

I borrowed the code from here: Archive rows to a separate sheet - google sheets
but basically I am trying to create an automatic archive where the rows of info delete from my master list on the source sheet but ONLY when I change column C to "Closed".
please help
Here is my code:
function onEdit() {
var ss = SpreadsheetApp.openByUrl('source sheet link');
//Logger.log(ss.getName());
var s = ss.getActiveSheet();
var r = ss.getActiveRange();
if(s.getName() == "Master List" && r.getColumn() == 3 && r.getValue() == "Closed");{
var targetSs = SpreadsheetApp.openByUrl('target sheet link');
//Logger.log(targetSs.getName());
var targetSheet = targetSs.getSheetByName("Archive");
var row = r.getRow();
var lastRow = r.getLastRow(); //save the last edited row index
var numColumns = s.getLastColumn();
var numRows = lastRow-row+2;
//Get data to be moved
var data = s.getRange(row, 1, numRows, numColumns).getValues();
//Paste the values to the target sheet on another spreadsheet
targetSheet.getRange(targetSheet.getLastRow()+1,1,data.length,data[0].length).setValues(data);
//Delete data in the source sheet
s.deleteRows(row, numRows);
}
}

Related

Archive rows to a separate sheet - google sheets

I have a script for archiving rows into one tab to another tab based on cell edit within the same spreadsheet. However the size of the sheet is getting very large resulting in slow page load. I was just wondering, as I'm not too experienced in scripts, if it were to possible to "archive" rows into a completely separate google sheet (new URL).
Note: At the moment, if you change Column 6 cell value to "Archive" it sends from one sheet ("Content Production Master") to another sheet ("Content Archive").
See code below for what I've got at the moment - any help would be very useful!
// assumes source data in sheet named Content Production Master
// target sheet of move to named Content Archive
// test column with "Archive" is col6
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Content Production Master" && r.getColumn() == 6 && r.getValue() == "Archive") {
var row = r.getRow();
var lastRow = r.getLastRow(); //Save the last edited row index
var numColumns = s.getLastColumn();
var numRows = lastRow-row + 1; // Corrected this calculation
var targetSheet = ss.getSheetByName("Content Archive");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
//s.getRange(row, 1, 1, numColumns).moveTo(target);
s.getRange(row, 1, numRows, numColumns).moveTo(target); // Take in account the lastRow when archiving the edited range.
//s.deleteRow(row);
for (let i = row; i<=lastRow; i++) {
s.deleteRow(row);
console.log(`Deleted row ${row} at pass number ${i}`);
}
}
}
Since it is not possible to move data from your source sheet to a different spreadsheet using move(target). You can manually get the data to be moved and paste that to another spreadsheet using getValues() and setValues(values).
Sample Code:
function onEdit(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
var r = ss.getActiveRange();
if(s.getName() == "Content Production Master" && r.getColumn() == 6 && r.getValue() == "Archive") {
//var targetSs = SpreadsheetApp.openById('1xh0huhm7ghGP7Y_5jNSFh7eaNX4iVoOL4SY95IWqpfs');
var targetSs = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1xh0huhm7ghGP7Y_5jNSFh7eaNX4iVoOL4SY95IWqpfs/edit?resourcekey=0-j6u0gdSEBp-F9SZYlZYi1g#gid=93990300');
var targetSheet = targetSs.getSheetByName("Content Archive");
var row = r.getRow();
var lastRow = r.getLastRow(); //Save the last edited row index
var numColumns = s.getLastColumn();
var numRows = lastRow-row + 1; // Corrected this calculation
//Get data to be moved
var data = s.getRange(row, 1, numRows, numColumns).getValues();
//Paste the values to the target sheet on another spreadsheet
targetSheet.getRange(targetSheet.getLastRow()+1,1,data.length,data[0].length).setValues(data);
//Delete data in the source sheet
s.deleteRows(row, numRows);
}
}
Pre-requisite(use installable onEdit trigger):
What it does?
After checking if the modified cell is in column 6 with value "Archive". Open the target spreadsheet using either openByUrl(url) or openById(id). Then select the target sheet.
Get the row values of the range to be moved using getValues()
Write the values to your target sheet using setValues(values)
Delete the archived rows in your source sheet using deleteRows(rowPosition, howMany)
Output:
Source Sheet (Before)
Source Sheet (After)
Target Sheet (After)

Move a row between different spreadsheets - Google Spreadsheet

I'm a beginner in coding and on SO, but I'm trying to understand what I'm doing and what is happening in the code.
I'm wanting to move a row between different spreadsheets.
I have a script in "CORE" Spreadsheet, which moves a row between the "Home" and "Finished" sheets Link to the spreadsheet:
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if (s.getName() == "Home" && r.getColumn() == 5 && r.getValue() == true) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Finished");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
else if (s.getName() == "Finished" && r.getColumn() == 5 && r.getValue() == false) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Home");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
I searched how to move the data located on "Finished" sheet to another spreedsheet called "DATA BASE" Link to the spreadsheet:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Finished");
var range = sheet.getRange("A1:E2");
var values = range.getValues();
var allvals = [];
for (var i = 0; i < values.length; i++) {
allvals.push(values[i]) ;
}
var dataLength = allvals.length;
// alert data
var ui = SpreadsheetApp.getUi();
// ui.alert( JSON.stringify(allvals) )
// copy to another Google Spreadsheet
var newSheet = SpreadsheetApp.openById('1Xn-hrPPr3tQwWMbKdWxHTXcazOqWu1bdHT6A5LZmlD8').getSheets()[0];
var tmp = "A1:E" + dataLength;
var newRange = newSheet.getRange(tmp);
newRange.setValues(values);
}
But I want to transfer the data from sheet "CORE"/"Finished" to sheet "DATA BASE"/ "DB" without rewriting the other rows already on the DB sheet. My idea is to make the spreadsheet "DATA BASE" a real database, where other sheets put data in it and other sheets use this data.
Is it possible to move rows between different spreadsheets like in the first code? I know its impossible to use the .openById() with the function onEdit(), but the most important thing is to move the rows without rewriting the other rows already in the target sheet (DB).
Moving a row to another spreadsheet with installable onEdit trigger
This is just an example of moving the data in a row from one spreadsheet to another with an installable onEdit Trigger. If you wish to play with it don't forget to add the target spreadsheet id.
function onMyEdit(e) {
//e.source.toast('Entry');
var sh=e.range.getSheet();
//Logger.log(JSON.stringify(e));
if(sh.getName()=='Sheet2' && e.range.columnStart==11 && e.value=="TRUE") {
//e.source.toast('Access');
e.range.setValue('FALSE');
var tss=SpreadsheetApp.openById('SSID');
var tsh=tss.getSheetByName('Sheet2');
var v=sh.getRange(e.range.rowStart,1,1,sh.getLastColumn()-1).getValues();
Logger.log(v);
tsh.getRange(tsh.getLastRow()+1,1,v.length,v[0].length).setValues(v);
sh.deleteRow(e.range.rowStart);
}
}

Google Sheets - Copy row script copies entire sheet

Obviously I have a small error as the code used copies the entire page, not just the row.
What I am trying to achieve is whenever "Green" is entered in Column 4 on Sheet "Jobs", the script copies that row to the next empty row on Sheet "Fleet"
I was also wondering if there is a way to possibly shade the copied row as a confirmation it has been copied to the "Fleet Sheet"
Here is the code so far:
function onEdit()
{
var sheetNameToWatch = "Jobs";
var columnNumberToWatch = 4;
var valueToWatch = "Green";
var sheetNameToCopyTheRowTo = "Fleet";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = sheet.getActiveCell();
if (sheet.getName() == sheetNameToWatch && range.getColumn() ==
columnNumberToWatch && range.getValue() == valueToWatch)
{
var targetSheet = ss.getSheetByName(sheetNameToCopyTheRowTo);
var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
sheet.getRange(2, 1, sheet.getLastRow(),
sheet.getLastColumn()).copyTo(targetRange);
}
}
Here is the link to an open copy of the spreadsheet (no important data so feel free to edit):
https://docs.google.com/spreadsheets/d/138PBXEsKdhhxkKNTCGaFaWgrOTQcnN3tPWHO8pp7GMg/edit#gid=610845771

Copying row to another google sheet by ID - NOT WORKING

Im trying to copy rows with the word "lead" from the 8th column to another spreadsheet with different ID and I am not getting any results at all. This is what I was able to do so far. I can not find the error of the code. Can you guys help me find it.
function onEdit(event) {
// assumes source data in sheet named Needed
// target sheet of move to named Acquired
// test column with lead is col 8 or H
// target sheet Id is 1nMpOJrq79UZPI3T9ykc415pAHIODg2he5xqi-DY6Wt0
// sheet name of target Id is Sheet1
var ss = SpreadsheetApp.openById("1nMpOJrq79UZPI3T9ykc415pAHIODg2he5xqi-DY6Wt0");
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "List" && r.getColumn() == 8 && r.getValue() == "lead") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Sheet1");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target);
}
}
Use range.setValues() instead of copyTo.
Note that the format is not copied, only the values.
I changed it that the dimensions of the range match that of the target range.
function onEdit(event) {
// assumes source data in sheet named Needed
// target sheet of move to named Acquired
// test column with lead is col 8 or H
// target sheet Id is 1nMpOJrq79UZPI3T9ykc415pAHIODg2he5xqi-DY6Wt0
// sheet name of target Id is Sheet1
var ss = SpreadsheetApp.openById("1nMpOJrq79UZPI3T9ykc415pAHIODg2he5xqi-DY6Wt0");
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "List" && r.getColumn() == 8 && r.getValue() == "lead") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Sheet1");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1, 1, numColumns);
target.setValues(s.getRange(row, 1, 1, numColumns).getValues());
}
}

Google App Script Spreadsheet Copy and Paste Row is Duplicating Row on Paste

Google Spreadsheet Code has been created to copy and paste a row of data into a target sheet in the same spreadsheet. However, when the row is being pasted into target sheet it is pasting it twice. I need to only have it pasted into the target location once... Not Twice.
Please See Sheet: https://docs.google.com/spreadsheets/d/1dL9FkxPawP9kvqJ4SwfRLqcv9HAUjHhZCfG9wxOwlDY/edit?usp=sharing
Script:
function onEdit(event)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
var curDat = new Date();
var valSent = r.getValue().toString().toLowerCase();
if(s.getName().toString() == "Referrals 2015" && r.getColumn() == 15 && valSent == "sent" && valSent!="")
{
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Auths and F&D");
var target = targetSheet.getRange(targetSheet.getLastRow()+1, 1);
var target1 = targetSheet.getRange(targetSheet.getLastRow()+1, 3);
var target2 = targetSheet.getRange(targetSheet.getLastRow()+1, 4);
var target3 = targetSheet.getRange(targetSheet.getLastRow()+1, 5);
var tRow =targetSheet.getLastRow();
s.getRange(row,1,1,2).copyTo(target);
s.getRange(row,5,1, 1).copyTo(target1);
s.getRange(row,12,1, 1).copyTo(target2);
s.getRange(row,13,1, 2).copyTo(target3);
targetSheet.getRange(tRow+1, 7).setValue(curDat);
targetSheet.getRange("B" +targetSheet.getLastRow()+3+":Z"+targetSheet.getLastRow()+3).copyTo(targetSheet.getRange("B" + targetSheet.getLastRow()), {formatOnly: true});
targetSheet.getRange(tRow+1,1,1,7).setBackgroundColor("#ffff00");
r = null;
s = null;
valSent="1";
}
}
onEdit() runs automatically as a simple trigger. If you have it set up in the scripts triggers from Resources>This project triggers, you need to remove it as it will run once as a simple trigger and again as an installed trigger.