Moving a row from one sheet to another sheet? - google-apps-script

Ok I am completely new to all of this so understand I know nothing.
I am working in google spreadsheet and I want when I mark a job Done in column I it moves the row to another spreadsheet. I have to sheets set up just don’t know how to make work.
I don’t know what script to use or how to import it the right way.
Can someone help?
I have tired a few script I found but get confused on where to put what information for it to work.

Move to Destination when marked done:
function onEdit(e) {
const jobdoneColumn = 1;
const sh = e.range.getSheet();
if(sh.getName() == "your sheet name" && e.range.columnStart == 1 && e.value == "Done") {
let vs = sh.getRange(e.range.rowStart,1,1,sh.getLastColumn()).getValues();
let dsh = e.source.getSheetByName("Destination Name");
dsh.getRange(dsh.getLastRow() + 1, 1,vs.length,vs[0].length ).setValues(vs);
sh.deleteRow(e.range.rowStart);
}
}

Related

Appscript help onedit email

I'm pretty new with Appscript and i'm looking for a solution when I complete a task, appscript code on edit will allow me to email whatever email is in the column B with just a simple task email that states the task has been completed.
This script does work but it emails just me whenever there is a change triggered. I'd like it just to email the email address in Column B and not me.
i've tried several different scripts but i don't know it well enough to pinpoint the issue or even edit it to fit my needs
function checkValue()
{
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName("Sheet1");
var valueToCheck = sheet.getRange("J:J").getValue();
if(valueToCheck="Approved")
{
MailApp.sendEmail("joes#nhcares.com","Task Completed","1231323"+
valueToCheck+ ".");
}
}
this may be too simple of a script, it does email me on edit but I need to tweak it more
Try this:
function checkValue(e) {
var sh = e.range.getSheet();
if (sh.getName() == "Sheet1" && e.range.columnStart == 10 && e.range.rowStart > 1 && e.value == 'Approved') {
let msg = `${sh.getRange(e.range.rowStart,1,1,sh.getLastColumn()).getDisplayValues().flat().join('\n')}`
MailApp.sendEmail(sh.getRange(e.range.rowStart,2).getDisplayValue(), "Task Completed", msg);
}
}
Use installable onEdit trigger

Google Sheets: Append then Delete rows based on Tickbox condition

I'm attempting to create a spreadsheet to organise products ordered at my workplace.
When an order is received a team member would add the details to the sheet; when it is collected they'd fill out date and ID then tick the order complete. See Attached
What I want to happen next is that the row containing the complete details from that order is appended to a second page in the sheet and the original row is deleted.
I can't make sense of how to get this to run automatically when the box is checked; so far I have been compiling a script to run from a button press:
function runFiling() {
function moveRows() {
var ss = SpreadsheetApp.getActive();
var osh = ss.getSheetByName('Current');
var dsh = ss.getSheetByName('Collected');
var srg = osh.getDataRange('H2:H');//You might want to specify a more unique range. This just gets all of the data on the sheet
var svA = srg.getValues();
var d=0;//deleted row counter
for(var i=1;i<svA.length;i++) {
if(svA[i][7] =='TRUE') {
dsh.appendRow(svA[i]);//append entire row to Sheet2
osh.deleteRow(i-d+1);//accounts for the difference between length of array and number of remaining row.
d++;
}
}
}
}
However even this fails to Append or Delete anything although no errors are found/returned.
If anyone can suggest a way to fix the above or, preferably, how to make the script work when the box is ticked your help will be most appreciated.
Try it this way using an onEdit(e) function
function onEdit(e) {
const sh = e.range.getSheet();
if (sh.getName() == 'Current' && e.range.columnStart == 7 && e.range.rowStart > 1 && e.value == "TRUE") {
const dsh = ss.getSheetByName('Collected');
const vs = sh.getRange(e.range.rowStart, 1, 1, sh.getLastColumn()).getValues()
dsh.getRange(dsh.getLastRow() + 1, 1, vs.length, vs[0].length).setValues(vs);
sh.deleteRow(e.range.rowStart);
}
}
This will accomplish the task line by line as the check boxes are checked off by the user.

OnEdit trigger fired simultaneously causing wrong calculation of last row

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

how do I move a row from one tab in a google sheet to another in the same sheet

I am super new (as in this is my first stab at anything code related) to this...
I've read a bunch of similar questions and I have watched several videos related to the specific thing I am trying to do. I thought it would be simple, but it keeps failing and I cannot figure out why!
I am trying to set it up so that when a checkbox in the row is checked (which currently automatically happens when a value is 0 in another cell), that entire row gets moved to bottom of the list in a different sheet tab within the same spreadsheet and deleted from the first sheet tab.
I know about filtering and every other simpler option, but for what we are doing, it won't work right. It needs to move.
The error I keep getting says: Error Message
TypeError: Cannot read property 'columnStart' of undefined
at onEdit(Move Row When Paid:4:51)
Here is what I have (pic also attached): Code
function onEdit(e) {
const src = e.source.getActiveSheet();
const r = e.range;
if (src.getName() != "Open Invoices" || r.range.columnStart != 11 || r.range.rowStart == 1) return;
const dest = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Closed Invoices");
src.getRange(r.rowStart,1,1,12).moveTo(dest.getRange(dest.getLastRow()+1,1,1,12));
src.deleteRow(r.rowStart);
}
I can link the spreadsheet if needed! All help is appreciated for this noob :)
Try it this way:
function onEdit(e) {
//e.source.toast("Entry")
const sh = e.range.getSheet();
if (sh.getName() == "Open Invoices" && e.range.columnStart == 11 && e.range.rowStart > 4 && e.value == "TRUE") {
//e.source.toast('Flag1');
const dest = e.source.getSheetByName("Closed Invoices");
sh.getRange(e.range.rowStart, 1, 1, 12).moveTo(dest.getRange(dest.getLastRow() + 1, 1));
sh.deleteRow(e.range.rowStart);
}
}
Here's was my sheet looks like:
I don't have your headers so it does not work below line 5

Google Spreadsheet Script - Increasing value of a cell in sheet2 before clearing cell in sheet1

Completely new when it comes to scripting etc but managed to get some small scripts working via looking up things here and there, so please bear with me.
I am looking to create a script that allows me to enter a value in a cell in sheet1 that adds the value to a cell in sheet2 (so not a simply copy over). Once the value in sheet1 has been entered, it clears itself.
Sadly, I have no script to show you of what I have tried, as this has me completely stumped. If anyone has any pointers or can provide help, it would be much appreciated.
Edit:
Based on below code, I've tried something like this:
function onEdit(e) {
//e.source.toast('Entry');//used for debugging
//Logger.log(JSON.stringify(e));//get event object
const sh = e.range.getSheet();
if(sh.getName() == "Dashboard" && e.getRange("C17") && e.value) {
let rg = e.source.getSheetByName("Data").getRange("D2")
rg.setValue(Number(rg.getValue()) + Number(e.value));
e.range.setValue('');
}
}
Though not getting it to work.
Add B2 Sheet0 to B2 Sheet1
function onEdit(e) {
//e.source.toast('Entry');//used for debugging
//Logger.log(JSON.stringify(e));//get event object
const sh = e.range.getSheet();
if(sh.getName() == "Sheet1" && e.range.columnStart == 2 && e.range.rowStart == 2 && e.value) {
let rg = e.source.getSheetByName("Sheet2").getRange(e.range.rowStart,e.range.columnStart)
rg.setValue(Number(rg.getValue()) + Number(e.value));
e.range.setValue('');
}
}
Note: you cannot run this type of function from the script editor just paste into script editor, save and go to Sheet1 B2 and type in a number and hit return. Then look on Sheet2 to see B2 change.
onEdit trigger