Javascript 检查日期并使用Google脚本编辑Google工作表中的相邻单元格
我正在尝试编写一个脚本,在循环包含日期的列的单元格值时,如果该日期等于或小于今天的日期,则该脚本将更改相邻单元格中当前与过期的状态。脚本需要处理N列日期,并修改电子表格中所有工作表的O列状态。这就是为什么我在那里有循环表供参考 这就是我到目前为止所做的,我只是不断地碰壁 它当前正在向currentValue变量抛出一个超出范围的错误Javascript 检查日期并使用Google脚本编辑Google工作表中的相邻单元格,javascript,google-apps-script,google-sheets,Javascript,Google Apps Script,Google Sheets,我正在尝试编写一个脚本,在循环包含日期的列的单元格值时,如果该日期等于或小于今天的日期,则该脚本将更改相邻单元格中当前与过期的状态。脚本需要处理N列日期,并修改电子表格中所有工作表的O列状态。这就是为什么我在那里有循环表供参考 这就是我到目前为止所做的,我只是不断地碰壁 它当前正在向currentValue变量抛出一个超出范围的错误 //---------------------------------------------------- // Look at Dates and Chang
//----------------------------------------------------
// 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");
}
}
}
}
//-----------------------------------------------------
for(x = 1; x <= numRows; ++x){ ...
currentValue超出范围的原因是getCellx,2函数的第一个参数是行号。您的行号从0开始,x=0。如果您将x更改为从1开始,它将停止提供currentValue变量超出范围的错误
//----------------------------------------------------
// 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");
}
}
}
}
//-----------------------------------------------------
for(x = 1; x <= numRows; ++x){ ...
正如我前面提到的,您的数据范围仅在第N列之上,如果同时选择第N列和第O列,var dateRange=sheets[v],则可以更轻松地使用它;
我对脚本的其余部分做了一些修改。它不漂亮,但我真的希望它能帮助你
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");
}
}
}
}
currentValue超出范围的原因是getCellx,2函数的第一个参数是行号。您的行号从0开始,x=0。如果您将x更改为从1开始,它将停止提供currentValue变量超出范围的错误
//----------------------------------------------------
// 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");
}
}
}
}
//-----------------------------------------------------
for(x = 1; x <= numRows; ++x){ ...
正如我前面提到的,您的数据范围仅在第N列之上,如果同时选择第N列和第O列,var dateRange=sheets[v],则可以更轻松地使用它;
我对脚本的其余部分做了一些修改。它不漂亮,但我真的希望它能帮助你
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");
}
}
}
}
太好了,谢谢你!我唯一有点担心的是所有的空单元格都被包含进去了。假设有开始,在运行日期检查循环之前,我只想从范围中删除5行数据。我已经更新了我的答案,以排除工作表末尾的空行。非常感谢!太好了,谢谢你!我唯一有点担心的是所有的空单元格都被包含进去了。假设有开始,在运行日期检查循环之前,我只想从范围中删除5行数据。我已经更新了我的答案,以排除工作表末尾的空行。非常感谢!