I'm attempting to write a script that will change the status (Current vs Expired) in an adjacent cell when looping over the cell values of a column containing dates if the date is equal to or less than today's date. The script needs to work on column N (dates) and modify column O (statuses) across ALL sheets in the spreadsheet. That is why I have the Sheets loop in there FYI.
Here is what I have so far and I just keep hitting walls.
It's currently throwing an error at the currentValue variable for being out of range.
//----------------------------------------------------
// Look at Dates and Change Status if expired (Automatically)
function checkDates() {
//For each sheet in the Spreadsheet
for(v in sheets){
//Find the last row that has content *-2 is because of a strange return I don't understand yet
var lastRow = sheets[v].getLastRow()-2;
//Get Dates Range (excluding empty cells)
var dateRange = sheets[v].getRange("N2:N"+lastRow);
//Get the number of Rows in the range
var numRows = dateRange.getNumRows();
//For loop for the number of rows with content
for(x = 0; x <= numRows; ++x){
// Value of cell in loop
var currentValue = dateRange.getCell(x,2).getValue();
Logger.log(currentValue);
// Row number in Range
var currentRow = dateRange.getRow(x);
// Get adjacent cell Range
var adjacentCell = sheets[v].getRange(currentRow,15);
// If the date is less than or equal to today
if(new Date(currentValue) <= d){
// Change adjancet cell to Expired
adjacentCell.setValue("Expired");
// Else adjance cell is Current
} else if(listofDates != ""){
adjacentCell.setValue("Current");
}
}
}
}
//-----------------------------------------------------
The reason for currentValue to be out of range is that the getCell(x, 2)
function first parameter is the row number. Your row number starts at 0, x = 0
. If you change x to start at 1 it should stop giving you the error that the currentValue variable is out of range.
for(x = 1; x <= numRows; ++x){ ...
You are also selecting 2 columns across but you only selected out of row "N", change getCell(x, 2)
to getCell(x, 1)
.
var currentValue = dateRange.getCell(x,1).getValue();
As I mentioned before your data range is only over colmn "N", it can make it easier if you select both column "N" and "O", var dateRange = sheets[v].getRange("N2:O");
I modified the rest of your script a bit. It is not pretty but I do hope it helps you.
function checkDates() {
//For each sheet in the Spreadsheet
for(v in sheets){
var lastRow = sheets[v].getLastRow();
//Get Dates Range (excluding empty cells)
var dateRange = sheets[v].getRange(2, 14, (lastRow - 1), 2);
//Get the number of Rows in the range
var numRows = dateRange.getNumRows();
//For loop for the number of rows with content
for(x = 1; x <= numRows; ++x){
// Value of cell in loop
var currentValue = dateRange.getCell(x,1).getValue();
var adjacentCell = dateRange.getCell(x,2);
Logger.log(currentValue);
// If the date is less than or equal to today
if(new Date(currentValue) <= new Date()){
// Change adjancet cell to Expired
adjacentCell.setValue("Expired");
// Else adjance cell is Current
} else if(currentValue != ""){
adjacentCell.setValue("Current");
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.