I have used a code found in another post (thank you very much, see below), to make a sheet open on Today's date.
Please, could you advise where I would need to add "+7" (or something else) to make it open on the date a week from now (Today + 7 days)?
How do I get my google spreadsheet to open to today's date?
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = sheet.getRange("1:1");
var values = range.getValues();
values = values[0];
var day = 24*3600*1000;
var today = parseInt((new Date().setHours(0,0,0,0))/day);
Logger.log(today);
var ssdate;
for (var i=0; i<values.length; i++) {
try {
ssdate = values[i].getTime()/day;
}
catch(e) {
}
if (ssdate && Math.floor(ssdate) == today) {
sheet.setActiveRange(range.offset(0,i,1,1));
break;
}
}
}
Try this:
function onOpen() {
var ss=SpreadsheetApp.getActive();
var sheet=ss.getActiveSheet();
var range=sheet.getRange("1:1");
var values=range.getValues()[0];
var today=new Date();
var nextweek=new Date(today.getFullYear(),today.getMonth(),today.getDate()+7);
for (var i=0;i<values.length;i++) {
var ssdate = values[i].getTime()/86400000;
if (ssdate && Math.floor(ssdate) == nextweek) {
sheet.setActiveRange(range.offset(0,i,1,1));
break;
}
}
}
Related
I want to be able to copy rows from a source sheet to a destination sheet that is the date in a range equals a target date.
I am using the following script and works perfectly when the target is a 'text', but as soon as I try to use a 'date' I get the following error: TypeError: Cannot read property "length" from undefined.
function copyrange() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Source'); //source sheet
var testrange = sheet.getRange('F:F');
var testvalue = (testrange.getValues());
var csh = ss.getSheetByName('Destination'); //destination sheet
var data = [];
var j =[];
//Condition check in L:L; If true copy the same row to data array
for (i=0; i<testvalue.length;i++) {
if ( testvalue[i] == 'target') {
data.push.apply(data,sheet.getRange(i+1,1,1,45).getValues());
//Copy matched ROW numbers to j
j.push(i);
}
}
//Copy data array to destination sheet
csh.getRange(csh.getLastRow()+1,1,data.length,data[0].length).setValues(data);
}
Thanks
Simon
I wasn't sure what your target was so I assumed current date and proceeded anyway. If it's wrong let me know.
function copyrange() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Source');
var vA=sh.getRange(1,1,sh.getLastRow(),sh.getLastColumn()).getValues();
var csh=ss.getSheetByName('Destination');
var found=false;
var dt=new Date();
var today=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();//midnight
var output=[];
for (var i=0;i<vA.length;i++) {
var date=new Date(vA[i][4]);//can be a string the constructor recognizes or a date
var dv=new Date(date.getFullYear(),date.getMonth(),date.getDate()).valueOf();//midnight
if (dv==today) {
output.push(vA[i]);
var found=true;
}
}
if(found) {
csh.getRange(csh.getLastRow()+1,1,output.length,output[0].length).setValues(output);
}else{
SpreadsheetApp.getUi().alert('No matches');
}
}
The following function will get those rows that have todays date and this email email1#test.com.
function copyrange(target) {
var target=target||'email1#test.com';
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Source');
var vA=sh.getRange(1,1,sh.getLastRow(),sh.getLastColumn()).getValues();
var csh=ss.getSheetByName('Destination');
var found=false;
var dt=new Date();
var today=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();//midnight
var output=[];
for (var i=0;i<vA.length;i++) {
var date=new Date(vA[i][4]);//can be a string the constructor recognizes or a date
var dv=new Date(date.getFullYear(),date.getMonth(),date.getDate()).valueOf();//midnight
if (dv==today && vA[i][6]==target) {
output.push(vA[i]);
var found=true;
}
}
if(found) {
csh.getRange(csh.getLastRow()+1,1,output.length,output[0].length).setValues(output);
}else{
SpreadsheetApp.getUi().alert('No matches');
}
}
And you can change the target email by passing an email to the function.
This function covers dates that have occurred in the last week:
function copyrange(target) {
var target=target||'email1#test.com';
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Source');
var vA=sh.getRange(1,1,sh.getLastRow(),sh.getLastColumn()).getValues();
var csh=ss.getSheetByName('Destination');
var found=false;
var dt=new Date();
var today=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();//midnight
var sevendaysago=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()-7).valueOf();//midnight
var output=[];
for (var i=0;i<vA.length;i++) {
var date=new Date(vA[i][4]);//can be a string the constructor recognizes or a date
var dv=new Date(date.getFullYear(),date.getMonth(),date.getDate()).valueOf();//midnight
if (dv>=sevendaysago && dv<=today && vA[i][6]==target) {
output.push(vA[i]);
var found=true;
}
}
if(found) {
csh.getRange(csh.getLastRow()+1,1,output.length,output[0].length).setValues(output);
}else{
SpreadsheetApp.getUi().alert('No matches');
}
}
This function only copies B and G of the matching row.
function copyrange(target) {
var target=target||'email1#test.com';
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Source');
var vA=sh.getRange(1,1,sh.getLastRow(),sh.getLastColumn()).getValues();
var csh=ss.getSheetByName('Destination');
var found=false;
var dt=new Date();
var today=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();//midnight
var sevendaysago=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()-7).valueOf();//midnight
var output=[];
for (var i=0;i<vA.length;i++) {
var date=new Date(vA[i][4]);//can be a string the constructor recognizes or a date
var dv=new Date(date.getFullYear(),date.getMonth(),date.getDate()).valueOf();//midnight
if (dv>=sevendaysago && dv<=today && vA[i][6]==target) {
output.push([vA[i][1],vA[6]]);
var found=true;
}
}
if(found) {
csh.getRange(csh.getLastRow()+1,1,output.length,output[0].length).setValues(output);
}else{
SpreadsheetApp.getUi().alert('No matches');
}
}
I have named ranges with all the same number of rows (21), all named ranges begin with a cell containing a date. I would like to hide all rows below their date cell when that date is older than today's date. Or, hide the associated 21 rows immediately following the date. I am ignorant as to how to write scripts, but I will learn. I also found I cannot ask a question well, very sorry.
I found a script in stackoverflow to hide a row with a date earlier than today:
function hideRows() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName("MON");
var v = s.getRange("A:A").getValues();
var today = new Date();
today.setHours(0, 0, 0, 0);
for (var i = s.getLastRow(); i > 2; i--) {
var t = v[i - 1];
if (t != "") {
var u = new Date(t);
if (u < today) {
s.hideRows(i);
}
}
}
}
When the sheet is opened I expect the row with the date earlier than today to cause the next 21 rows to be hidden.
Try this:
This gets all of the ranges on the active sheet
function hideNamedRange() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet();
var rgA=sh.getNamedRanges();
var dv=new Date(new Date().getFullYear(),new Date().getMonth(),new Date().getDate()).valueOf();
for(var i=0;i<rgA.length;i++) {
var vA=rgA[i].getRange().getValues();
if(vA[0][0] && new Date(vA[0][0]).valueOf()<dv) {
sh.hideRows(rgA[i].getRange().getRow(), rgA[i].getRange().getHeight());
}
}
}
This does the same thing for the spreadsheet
function hideNamedRange() {
var ss=SpreadsheetApp.getActive();
var rgA=ss.getNamedRanges();
var dv=new Date(new Date().getFullYear(),new Date().getMonth(),new Date().getDate()).valueOf();
for(var i=0;i<rgA.length;i++) {
var rg=rgA[i].getRange();
var sh=rg.getSheet();
var vA=rg.getValues();
if(vA[0][0] && new Date(vA[0][0]).valueOf()<dv) {
sh.hideRows(rgA[i].getRange().getRow(), rgA[i].getRange().getHeight());
}
}
}
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++;
}
}
}
Here is a link to my document:
https://docs.google.com/spreadsheets/d/1wBpeCUTmePD3N5CDz57LeBBWZHnSwrX-5b6XscdbB4s/edit?usp=sharing
I want the document to open to today's date or to the Monday of each week.
Please help.
Thanks in advance.
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = sheet.getRange("A:A");
var values = range.getValues();
var day = 24*3600*1000;
var today = parseInt((new Date().setHours(0,0,0,0))/day);
var ssdate;
for (var i=0; i<values.length; i++) {
try {
ssdate = values[i][0].getTime()/day;
} catch(e) { }
if (ssdate && Math.floor(ssdate) == today) {
sheet.setActiveRange(range.offset(i,0,1,1));
break;
}
}
}
The main issue in your attempt is that you grabbed column A, but your dates exist in row 2. Try this:
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("2018"); // Specifiy the sheet
var range = sheet.getRange("2:2"); // Row 2
var values = range.getValues()[0]; // This is a 2-dimensional array in the form [[row1-col1, row1-col2],[row2-col1,row2-col2]].
// Because we only pulled one row (Row 2), we can immediately select just that row by appending the [0]
var today = new Date();
var todayDate = today.getDate();
var todayMonth = today.getMonth();
var todayYear = today.getFullYear();
for (var i=0; i<values.length; i++) {
var columnDate = new Date(values[i]);
if (columnDate.getDate() === todayDate && columnDate.getMonth() === todayMonth && columnDate.getFullYear() === todayYear) {
sheet.getRange(1, i+1).activate();
break;
}
}
}
Sample Sheet- So, I am new to this part. Our work schedule is in by-weekly format, each two-week pay-period is on a separate sheet.
I need a script to cause it to open to the current date whenever a user accesses the sheet from their Google Drive.
Similar to this, but something isn't right:
function onOpen() {
var today = new Date();
var ss = SpreadsheetApp.getActive();
ss.setActiveSheet(ss.getSheets()[today.getMonth()]);
}
This script should bring you to the sheet with the current date..
function onOpen() {
var today = format(new Date());
var ss = SpreadsheetApp.getActive();
var sheets = ss.getSheets()
.forEach(function (s) {
var val = s.getRange("C2:P2")
.getValues()[0];
for (var i = 0, len = val.length; i < len; i++) {
if (format(val[i]) === today) {
ss.setActiveSheet(s)
break;
}
}
});
}
function format(date) {
return formattedDate = date.getMonth() + 1 + "/" + date.getDate()
}
See if this works ?