I'm currently trying to make a script or literally anything that will be able to delete a row after the given date in Column C.
The site is a giveaway site so I need the rows/entries to delete themselves once the date specified on Column C is passed.
Eg: If one giveaway had an expiration date # 20/13/2016, once the date reaches this date of 20/13/2016 it will delete the row. I am following the metric system of dd/mm/yy as a note.
I saw a question similar to this at Google Sheets - Script to delete date expired rows but the code won't work for my needs.
Here is the code that was used in the other question.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Foglio1");
var datarange = sheet.getDataRange();
var lastrow = datarange.getLastRow();
var values = datarange.getValues();// get all data in a 2D array
var currentDate = new Date();
var oneweekago = new Date();
oneweekago.setDate(currentDate.getDate() - 7);
for (i=lastrow;i>=2;i--) {
var tempdate = values[i-1][2];// arrays are 0 indexed so row1 = values[0] and col3 = [2]
if(tempdate < oneweekago)
{
sheet.deleteRow(i);
}
}
}
If you could change it to work for my above needs it will be greatly appreciated!
Assuming your dates are in column C as stated, this should do it. The adjustment is just to the date to which we compare and to handle missing dates. I am also messing with the case on some names for readability.
function DeleteOldEntries() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Live Events");//assumes Live Events is the name of the sheet
var datarange = sheet.getDataRange();
var lastrow = datarange.getLastRow();
var values = datarange.getValues();// get all data in a 2D array
var currentDate = new Date();//today
for (i=lastrow;i>=3;i--) {
var tempDate = values[i-1][2];// arrays are 0 indexed so row1 = values[0] and col3 = [2]
if ((tempDate!=NaN) && (tempDate <= currentDate))
{
sheet.deleteRow(i);
}//closes if
}//closes for loop
}//closes function
Related
Objective
Get the rows where the dates in column A are within the range of this current month and year. In the rows that coincide in month and year with the current date, the value "yes" will be placed in the cell of column O (col. 15 - Array 14). The non-matching rows will be placed the value "no" in the cell of column 0 (col.15). Finally, checkboxes will be created for the entire column O (col.15), and depending on the value of the cell, the checkboxes will be marked or not.
Problem
I am getting an error "Exception: Service error: Spreadsheets" in the line of code dataRange.setValues(dataValues); And I don't know why or how to fix it.
This error in GAS is not explained any more. I have looked for solutions online but despite following the instructions, I can not solve this.
My code
function thisMonth() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('π
Todos los eventos');
const lastRow = sheet.getLastRow();
const lastCol = sheet.getLastColumn();
var dataRange = sheet.getRange(2, 1, lastRow -1, lastCol);
var dataValues = dataRange.getValues();
const now = new Date();
const month = now.getMonth();
const year = now.getFullYear();
dataValues.forEach((fila)=> {
var dateColA = new Date(fila[0]);
if ( month == dateColA.getMonth() && year == dateColA.getFullYear() ){
fila[14] = 'yes';
Logger.log(dateColA + ' - ' + fila[3]);
} else {
fila[14] = 'no';
}
})
dataRange.setValues(dataValues);
sheet.getRange(2, 15, lastRow -1, 1).insertCheckboxes('yes');
}
I tested your code and it works with a small sample sheet I made. Not sure about your exact error but I've heard it could be related to reaching some kind of limit when handling Sheet data. With that in mind, you should try to optimize it. You don't need to get the entire x-rows by 15-columns range and manipulate it then completely rewrite it to the Sheet, when all you want is to edit the "O" column.
I suggest you instead try to optimize your code to only get the values from column "A" to compare the dates, then create a local array to build the "O" column and then set the values to just that column. Here's a sample that worked for me:
function thisMonth() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('π
Todos los eventos');
const lastRow = sheet.getLastRow();
const lastCol = sheet.getLastColumn();
var dataRange = sheet.getRange(2, 1, lastRow -1);
var checkboxRange = sheet.getRange(2, 15, lastRow -1)
var dataValues = dataRange.getValues();
var checkboxValues = []
const now = new Date();
const month = now.getMonth();
const year = now.getFullYear();
for (i = 0; i<dataValues.length; i++){
if (dataValues[i][0].getMonth()==month && dataValues[i][0].getFullYear()==year){
checkboxValues.push(["yes"])
}else{
checkboxValues.push(["no"])
}
}
checkboxRange.setValues(checkboxValues);
sheet.getRange(2, 15, lastRow -1).insertCheckboxes('yes');
}
Link of My sheet is :
https://docs.google.com/spreadsheets/d/1czJbRU5ELNft1IfGq1cABGe30j8BWjnffVCEa8A_AeY/edit?usp=sharing
I am trying to move data if N is equal to today. I have set the trigger. This script runs on time driven between 8 PM to 9 PM. It copies the data in Row 8 when column K onwards there is noting mentioned. In the current Payment Approval Sheet, while running the script it copies the data in 1500th row.
The script I am using is as below:
function copyrange() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Complete Invoice Sheet'); //source sheet
var testrange = sheet.getRange('N:N');
var testvalue = (testrange.setNumberFormat("#").getValues());
var ds = ss.getSheetByName('Payment Approval Sheet'); //destination sheet
var data = [];
var j =[];
var dt = new Date();
var today = Utilities.formatDate(new Date(), 'GMT-0', 'dd/MM/yyyy')
//Condition to check in N:N, if true, copy the same row to data array
for (i=0;i<testvalue.length;i++) {
if (testvalue[i] == today) {
data.push.apply(data,sheet.getRange(i+1,1,1,13).getValues());
//Copy matched ROW numbers to j
j.push(i);
}
}
//Copy data array to destination sheet
ds.getRange(ds.getLastRow()+1,1,data.length,data[0].length).setValues(data);
}
Issue:
Your current solution considers the last row of your destination
sheet Payment Approval Sheet. However, in that sheet, checkboxes
are populated in column N until the bottom of the sheet. Therefore,
getLastRow() returns the row at the bottom of column N which is not
what you want.
Explanation:
Instead of using getLastRow(), calculate the number of elements after cell A7 by using the filter() operation and then use this as a starting point when you copy & paste the data to the destination sheet:
var start_row=ds.getRange('A8:A').getValues().filter(String).length +7; //calculate max row
ds.getRange(start_row+1,1,data.length,data[0].length).setValues(data);
Solution:
function copyrange() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Complete Invoice Sheet'); //source sheet
var testrange = sheet.getRange('K:K');
var testvalue = (testrange.setNumberFormat("#").getValues());
Logger.log(testvalue);
var ds = ss.getSheetByName('Payment Approval Sheet'); //destination sheet
var data = [];
var j =[];
var dt = new Date();
var today = Utilities.formatDate(new Date(), 'GMT-0', 'dd/MM/yyyy')
//Condition to check in N:N, if true, copy the same row to data array
for (i=0;i<testvalue.length;i++) {
if (testvalue[i] == today) {
data.push.apply(data,sheet.getRange(i+1,1,1,13).getValues());
//Copy matched ROW numbers to j
j.push(i);
}
}
//Copy data array to destination sheet
var start_row=ds.getRange('A8:A').getValues().filter(String).length +7; //calculate max row
ds.getRange(start_row+1,1,data.length,data[0].length).setValues(data);
}
I have a google spreadsheet where some of the sheets names are text and others are dates in "dd/mm/yyyy/" format. I need a function that can put the sheet called "Tablero" first, then sort the sheets named with dates descendingly, and leave at the end the rest of the sheets.
This is my code so far:
function testOrdenar() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetsCount = ss.getNumSheets();
var sheets = ss.getSheets();
// I need the sheet "Tablero" to go first
var tablero = ss.getSheetByName("Tablero");
ss.setActiveSheet(tablero);
ss.moveActiveSheet(1);
var names = []; // This is where the sheets named with dates will go
var j = 1; // I use this as a counter, but it is not absolutly necessary
for (var i = 0; i<sheetsCount; i++){
var sheetName = sheets[i].getName();
var pattern = /(\d{2})\/(\d{2})\/(\d{4})/;
var dt = new Date(sheetName.replace(pattern,'$3-$2-$1'));
if (dt instanceof Date && !isNaN(dt.valueOf())) {names[j] = dt; j++;} // This is to distinguish the sheets that are dates
}
// Here I sort the sheets descendingly
for (var m = 1;m<j;m++) {
for (var n = 1;n<j;n++) {
if (names[n] < names[m]) {
var aux = names[n];
names[n] = names[m];
names[m] = aux;
}
}
}
var pos = 2;
for (var a = 0;a<j;a++) {
var sheetName = Utilities.formatDate(new Date(names[a]), "GMT-3", "dd/MM/yyyy");
Logger.log(sheetName);
var sheet = ss.getSheetByName(sheetName);
ss.setActiveSheet(sheet);
ss.moveActiveSheet(pos);
pos++;
}
The sorting is correct, but I don't know why each date ends up being a day less. I tried adding one to de variable but it comes out as "Invalid Object". And I also need those dates as strings because that is how i can then call the sheets.
My questions:
1) How can I get the correct dates? (not a day before each one). Could it have something to do with the timezone? I'm in "GMT-3".
(If the answer is adding one, please tell me how because I tried that and comes back as an error.)
2) How can I get the sorted dates as strings in "dd/MM/yyyy" format?
Here are the screenshots of my sheets and the logs I get:
Cause:
As written in the documentation, Date.parse(timestring), where timestring is a date only iso8601 compatible string, returns a date in UTC. When you format the date in GMT-3, the date is offset 3 hours to the previous day.
Solution:
Use GMT as timezone argument of Utilities.formatDate
Alternatively, You can avoid converting to date at all and sort them as plain strings.
Sample script:
function testOrdenar1() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheets = ss.getSheets();
const pattern = /(?:Tablero)|(\d{2})\/(\d{2})\/(\d{4})/;
const map = sheets.reduce( //Create a map of sheet object : sheetName(as yyyyMMdd)
(m, sheet) =>
m.set(
sheet,
sheet
.getName()
.replace(pattern, (m, p1, p2, p3) =>
m === 'Tablero' ? '99999999' : p3 + p2 + p1//make Tablero highest 8 digit number string
)
),
new Map()
);
sheets.sort((a, b) => map.get(b) - map.get(a));//sort sheet objects by their name
let pos = 1;
sheets.forEach(sh => {
ss.setActiveSheet(sh);
ss.moveActiveSheet(pos++);
});
}
I am writing a code that targets a column with dates and I would like to get the dates and compare it to the current date so that I can get the difference between the two.
I ran into a problem I am having trouble solving. It seems that when I used .getValues in my range, it placed each timestamp in an array then those arrays in another array. Like this: [[(new Date(1539619200000))], [(new Date(1540396800000))], [(new Date(1540828800000))]]
I would like to place all the values in 1 array only so I can start solving how to convert the timestamps in to normal dates. I am also a beginner with this so I am sorry if this seems like an basic question.
function datesincolumn() //collects only the date values in range
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var lastRow = sheet.getLastRow(); //will identify last row with any data
entered
var range = sheet.getRange('D2:D' + lastRow); //will get range of cells
with data present
var dates = range.getValues();//gets all values in the range
var CurrentDate = new Date();
var timestamps = [];
for (i = 0; i <= dates.length; i++)
{
if (dates[i] >= i)
{
timestamps.push(dates[i]);
}
}
Logger.log(dates);
}
When you use .getValues(), you will always get a 2-dimensional array. The outer array represents the row, and the inner array represents the columns. Since you're only getting one column, you can simply append [0] to your dates[i].
function datesincolumn() { //collects only the date values in range
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var lastRow = sheet.getLastRow(); //will identify last row with any data entered
var range = sheet.getRange('D2:D' + lastRow); //will get range of cells with data present
var dates = range.getValues();//gets all values in the range
var CurrentDate = new Date();
var timestamps = [];
for (var i = 0; i <= dates.length; i++) {
var date = dates[i][0];
if (date >= i)
timestamps.push(date);
}
Logger.log(dates);
}
function checkReminder() {
// get the spreadsheet object
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// set the first sheet as active
SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
// fetch this sheet
var sheet = spreadsheet.getActiveSheet();
// figure out what the last row is
var lastRow = sheet.getLastRow();
// the rows are indexed starting at 1, and the first row
// is the headers, so start with row 2
var startRow = 2;
// grab column 10 (the 'date end' column)
var range = sheet.getRange(2,10,lastRow-startRow+1,1 );
var numRows = range.getNumRows();
var date_end_values = range.getValues();
// Now, grab the Event name data
range = sheet.getRange(2, 5, lastRow-startRow+1, 1);
var reminder_info_values = range.getValues();
var warning_count = 0;
var msg = "Send out a follow-up email asking how the event was!";
}
//Get today's date
var todaysDate = new Date();
var numRows = numRows
// Loop over the days left values
for (var i = 0; i <= numRows - 1; i++) {
var date_end = date_end_values[i][0];
//call setHours to take the time out of the comparison
if(date_end == todaysDate.setHours(0,0,0,0)) {
MailApp.sendEmail("max#xpogo.com",
"Reminder Spreadsheet Message", msg);
}
What I'm trying to do is have gmail send me a reminder when a certain column in my data set is equal to the present date. Im new to coding and am running into trouble. Please help?
I added the following after your variables were set, to determine whether they were commensurate for your comparison (hit ctrl+Enter after executing to view the log).
Logger.log(date_end);
Logger.log(todaysDate.setHours(0,0,0,0));
They weren't comparable at all. Here are the lines I changed to make this work:
var todaysDate = Utilities.formatDate(new Date(), "GMT+1", "MM/dd/yyyy");
var date_end = Utilities.formatDate(date_end_values[i][0], "GMT+1", "MM/dd/yyyy");
if(date_end == todaysDate) {
HTH