Google App Script, Moving row onto another sheet if date has passed - google-apps-script

I've been working on this code and doing some research the last couple of weeks and not having much luck. I'm looking to setup a trigger that will scan Col C of dates and if the date has passed move that specific row to another sheet.
Below is where I'm currently at.
Note that I have today's date stored in cell 'O2' of the 'Contract Language' sheet.
I've gotten this code to work with text, but not with dates.
Any advise is greatly appreciated!
function MovetoPastShows2() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Confirmed Deals'); //source sheet
var testrange = sheet.getRange('C:C');
var testvalue = testrange.getValues();
var todaysheet = ss.getSheetByName('Contract Language');
var todaycol = todaysheet.getRange('O2');
var today = todaycol.getValues().valueOf();
var csh = ss.getSheetByName('Confirmed (Past)'); //destination sheet
var data = [];
var j =[];
for (i=0; i<testvalue.length;i++) {
if ( testvalue[i] = today) {
data.push.apply(data,sheet.getRange(i+1,1,1,187).getValues());
j.push(i);
}
}
csh.getRange(csh.getLastRow()+1,1,data.length,data[0].length).setValues(data);
for (i=0;i<j.length;i++){
var k = j[i]+1;
sheet.deleteRow(k);
if (!(i == j.length-1)) {
j[i+1] = j[i+1]-i-1;
}
}
};

Move Past Rows to Destination Sheet
function MovetoPastShows2() {
var ss = SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Confirmed Deals');
var rg=sh.getDataRange();
var vA=rg.getValues();
var today=new Date(ss.getSheetByName('Contract Language').getRange('O2').getValue()).valueOf();
var dsh=ss.getSheetByName('Confirmed (Past)');
var d=0;
for(var i=0;i<vA.length;i++) {
if (new Date(vA[i][2]).valueOf()<=today) {
dsh.appendRow(vA[i])
sh.deleteRow(i+1-d);
d++;
}
}
}

Related

Script for sheets to change a cell based on a value on a different cell

everyone!
I'm trying to write a script where the cell G3 on the sheet 'ref view' will change to 'Yes' if the cell D9 on 'view' contains 'BP' when I run it. The problem is that the execution is completed without errors but when I try to test it nothing happens.
This is the code:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var sheetInfo = ss.getSheetByName('ref view')
var data = sheet.getDataRange().getValues();
var rangeView = ss.getRange('View!D9');
var cell = ss.getRange('ref view!G3');
var range = rangeView;
for(var i=0; i<data.length; i++) {
if(rangeView == 'BP') {
sheet.getRange(cell).clear().setValue('Yes');
}
}
} ```
Try it this way:
function one() {
var ss = SpreadsheetApp.getActive();
var sh1 = ss.getActiveSheet();
var vs1 = sh1.getDataRange().getValues();
var sh2 = ss.getSheetByName('ref view')
var sh3 = ss.getSheetByName('view');
var sh3D9v = sh3.getRange('D9').getValue();//value
var sh2G3r = sh2.getRange('G3');//Range
for(var i=0; i<vs1.length; i++) {
if(sh3D9v == 'BP') {
sh1.getRange(sh2G3r).setValue('Yes');
}
}
}
Be aware that a range does not return a value without the use of a getValue() or a getValues();

How to make this script run faster?

Hello so I am trying to get values from the 'book' sheet then copy it to 'Completed' sheet. It takes the values form book stores it in the data array and copy them to the 'Completed' sheet. Its working fine but very slow which jeopardizes my work in time-wise. How can I make this run faster ?
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var clear_sheet = spreadsheet.getSheetByName('Completed_Orders'); // clear the destination sheet first
clear_sheet.getRange('A2:X').clear();
var sheet = spreadsheet.getSheetByName('book'); //source sheet
var Datarange = sheet.getRange('Q3:Q'); //range to check
var Datavalue = (Datarange.getValues());
var dest = spreadsheet.getSheetByName('Completed_Orders'); //destination sheet
var data = [];
for (i=0; i<Datavalue.length;i++) {
if ( Datavalue[i] == "Completed") {
data.push.apply(data,sheet.getRange(i+3,1,1,24).getValues());
}
}
dest.getRange(2,1,data.length,data[0].length).setValues(data);
var column = dest.getRange('A3:A');
var values = column.getValues(); // get all data in one call
var ct = 0;
while ( values[ct][0] != "" ) { // to find the last row correctly, getLastRow is not working perfectly
ct++;
}
var endRow = ct+2;
}
Try this:
Don't use this syntax var values=sheet.getRange('A3:A').getValues() because it get's the data all the way down to getMaxRows(). Instead use var values=sheet.getRange(3,1,sheet.getLastRow()-2,1).getValues().
Also Datavalue[i] is a whole row
function myfunction() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var clear_sheet = spreadsheet.getSheetByName('Completed_Orders');
clear_sheet.getRange(2,1,clear_sheet.getLastRow()-1,24).clear();
var sheet = spreadsheet.getSheetByName('book'); //source sheet
var Datarange = sheet.getRange(3,17,sheet.getLastRow()-2,1); //range to check
var Datavalue = Datarange.getValues();
var dest = spreadsheet.getSheetByName('Completed_Orders'); //destination sheet
var data = [];
for (var i=0;i<Datavalue.length;i++) {
if (Datavalue[i]["************You need another index here**************"] == "Completed") { //need another index Datavalue is 2d
data.push(sheet.getRange(i+3,1,1,24).getValues());
}
}
dest.getRange(2,1,data.length,data[0].length).setValues(data);
}
I think this version will be a lot faster.
function myfunction() {
var ss=SpreadsheetApp.getActiveSpreadsheet();
var ssh=ss.getSheetByName('book');
var dsh=ss.getSheetByName('Completed_Orders');
dsh.getRange(2,1,dsh.getLastRow()-1,24).clear();
var dv=ssh.getRange(3,1,ssh.getLastRow()-2,24).getValues();//this version just gets this data one time so it should a lot faster.
var data=[];
for (var i=0;i<dv.length;i++) {
if (dv[i][16]=="Completed") {
data.push(dv[i]);
}
}
dsh.getRange(2,1,data.length,data[0].length).setValues(data);
}

GoogleSheet Script to Move to Sheet with same name as cell value

I have a sheet with 6 columns. Row 1 is titles and each other sheet has the same titles and columns.
I have tried a couple codes like this that i found online with edits but can get it to correctly work for multiple sheets.
function copyrange() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Source');
var testrange = sheet.getRange('F:F');
var testvalue = (testrange.getValues());
var csh = ss.getSheetByName('target');
var data = [];
var j =[];
for (i=0; i<testvalue.length;i++) {
if ( testvalue[i] == 'Completed') {
data.push.apply(data,sheet.getRange(i+1,1,1,25).getValues());
//Copy matched ROW numbers to j
j.push(i);
}
}
csh.getRange(csh.getLastRow()+1,1,data.length,data[0].length).setValues(data);
for (i=0;i<j.length;i++){
var k = j[i]+1;
sheet.deleteRow(k);
if (!(i == j.length-1)) {
j[i+1] = j[i+1]-i-1;
}
}
Any Help would be greatly appreciated
Deleting Rows as you collect data that meet criteria
function moveRows() {
var ss=SpreadsheetApp.getActive();
var sheet=ss.getSheetByName('Source');
var vA=sheet.getRange(1,1,sheet.getLastRow(),sheet.getLastColumn()).getValues();
var csh=ss.getSheetByName('target');
var d=0;//deleted row counter
var data=[];
for(var i=0;i<vA.length;i++) {
if (vA[i][5]=='Completed') {
data.push(vA[i]);
sheet.deleteRow(i+1-d++);//increment deleted row counter as you delete rows
}
}
csh.getRange(csh.getLastRow()+1,1,data.length,data[0].length).setValues(data);
}
Sheet getLastRow()
Sheet getLastColumn

Functions all executing at the same time

I am trying to copy data from one sheet, paste it into another and then delete the row that have a "flag" attached to them (Column D). When I execute this code, it just deletes the flag and not the whole row. What is going on here? The functions seem to execute in the wrong order.
Tried using Utilities.sleep(), as well as
separating both into different functions
using a for loop writing out all the columns I want deleted
applying flush to the functions before executing the next
//copies the code
function myFunction() {
var url = 'https://docs.google.com/spreadsheets/d/1ie2Fyj2piVV8XC0XUGX-fTAHcQ0UUUdec4Mtr1QdXtQ/edit#gid=293227072'
var ss = SpreadsheetApp.openByUrl(url)
var sheet = ss.getSheetByName('Bad Emails Input')
var range = sheet.getRange("K:N")
var values = range.getValues()
var target = ss.getSheetByName("worklist")
target.getRange("A:D").setValues(values)
SpreadsheetApp.flush()
Utilities.sleep(5 * 1000)
}
//deletes the rows with flags in them
function myFunction2() {
var url = 'https://docs.google.com/spreadsheets/d/1ie2Fyj2piVV8XC0XUGX-fTAHcQ0UUUdec4Mtr1QdXtQ/edit#gid=293227072'
var ss = SpreadsheetApp.openByUrl(url)
var sheet = ss.getSheetByName('Bad Emails Input')
var range = sheet.getRange("K:N")
var values = range.getValues()
var target = ss.getSheetByName("worklist")
Utilities.sleep(6 * 1000)
var oldnews = target.getRange("D:D")
for (i = oldnews.length; i > 0; i--){
if (oldnews[i] !== undefined) {
target.getRange(i,1)
target.getRange(i,2)
target.getRange(i,3)
target.getRange(i,4)
}
}
SpreadsheetApp.flush()
}
It's not really clear what your asking. So I just guessed at some of this.
function myFunction() {
var ss = SpreadsheetApp.openById("id");
var sheet = ss.getSheetByName('Bad Emails Input')
var range = sheet.getRange(1,11,sheet.getLastRow(),4);
var values = range.getValues()
var target = ss.getSheetByName("worklist")
target.getRange(1,1,values.length,values[0].length).setValues(values);
SpreadsheetApp.flush();
var d=0;
for(var i=0;i<values.length;i++) {
if(values[i][3]) {
target.deleteRow(i+1-d++);
}
}
}
Try the below code:
function myFunction() {
var url = 'https://docs.google.com/spreadsheets/d/1ie2Fyj2piVV8XC0XUGX-fTAHcQ0UUUdec4Mtr1QdXtQ/edit#gid=293227072'
var ss = SpreadsheetApp.openByUrl(url);
var sheet = ss.getSheetByName('Bad Emails Input');
var range = sheet.getRange("K:N");
var values = range.getValues();
var target = ss.getSheetByName("worklist");
target.getRange("A:D").setValues(values);
SpreadsheetApp.flush();
//If you have a header row change the below to A2:D and count to 2
var data=ss.getSheetByName("worklist").getRange("A:D").getValues(),count=1;
for (i in data) {
var rowdata=data[i];
if (!rowdata[0] || rowdata[0]==''){break;}
if (rowdata[3] == 'flag') {//change the flag to however it is in column D
ss.getSheetByName("worklist").deleteRow(count);
count++;
}
}
}

Message Box if Cell Range is Today

I'm new to Google App Script and new to coding for Google Sheets.
I'm attempting to:
Have a pop-up box show if any date in a range of cells is equal to today.
Here's the code I have so far:
function onOpen()
{
var ss = SpreadsheetApp.getActiveSheet();
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Required Items List');
var ss = SpreadsheetApp.getActive();
var range = sheet.getRange('Required Items List!E8:G22');
var data = range.getValue();
var today = new Date();
Logger.log(data);
Logger.log(today);
if (data == today)
{
Browser.msgBox('Send Required Items Reminders Today!', Browser.Buttons.OK);
}
else {}
}
Range Example
It appears by data variable on reads the first cell for E8. I'm sure its a simple solution, I'm just missing some logic for it. Any help would be greatly appreciated.
You may code something like this :
function onOpen(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var lastrow = ss.getLastRow();
var lastcol = ss.getLastColumn();
var today = Utilities.formatDate(new Date(), "GMT+05:30", "''yyyy-MM-dd");
var flag = 'false';
for(var i =8; i<=22; i++){
for(var j =5; j<=7; j++){
var data = sheet.getRange(i, j, i, j).getValue();
var shDate = Utilities.formatDate(new Date(data), "GMT+05:30", "''yyyy-MM-dd");
if (shDate == today){
Browser.msgBox('Send Required Items Reminders Today!', Browser.Buttons.OK);
flag = 'true';}
if(flag == 'true')
break;
}
if(flag == 'true')
break;
}
}
Hope this will help you.
Thanks.