检查日期并使用谷歌脚本编辑谷歌表格中的相邻单元格
Check date and edit adjacent cell in Google Sheet with Google Script
我正在尝试编写一个脚本,如果日期等于或小于今天的日期,则在循环包含日期的列的单元格值时,该脚本将更改相邻单元格中的状态(当前与过期)。该脚本需要处理第 N 列(日期)并修改电子表格中所有工作表中的 O 列(状态)。这就是为什么我在那里有工作表循环的原因 仅供参考。
这就是我到目前为止所拥有的,我只是不断撞墙。
它当前在 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");
}
}
}
}
//-----------------------------------------------------
currentValue 超出范围的原因是 getCell(x, 2)
函数的第一个参数是行号。您的行号从 0 开始,x = 0
。如果将 x 更改为从 1 开始,它应该停止为您提供当前值变量超出范围的错误。
for(x = 1; x <= numRows; ++x){ ...
您还选择了 2 列,但您只选择了行"N",将getCell(x, 2)
更改为 getCell(x, 1)
。
var currentValue = dateRange.getCell(x,1).getValue();
正如我之前提到的,您的数据范围仅在colmn"N"上,如果您同时选择"N"和"O"列,则可以更轻松地var dateRange = sheets[v].getRange("N2:O");
我稍微修改了你脚本的其余部分。它并不漂亮,但我希望它对您有所帮助。
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");
}
}
}
}
相关文章:
- 在谷歌电子表格中循环单元格
- 如何使用图表包装函数有条件地格式化谷歌可视化表单元格
- 如何通过悬停或点击谷歌电子表格中的单元格来运行自定义功能
- getRange(单元格)在谷歌应用程序脚本中不起作用
- 谷歌工作表中的按钮,当单击时,会更改特定单元格的背景
- 谷歌应用程序脚本-onFormSubmit-复制到电子表格中的单元格
- 为什么谷歌应用程序的电子表格setValue函数没有在单元格中设置值
- 如何根据单元格中的值将一行从一个谷歌电子表格移动到另一个
- 如何在谷歌电子表格中检查单元格的类型
- 谷歌应用程序脚本:;范围不是函数,它是对象;尝试在单元格中放置注释时出错
- 谷歌电子表格在多个单元格中显示脚本中的值
- 谷歌应用程序-不;t在使用调试运行脚本期间填充电子表格单元格
- 谷歌应用程序将电子邮件表单脚本转换为PDF格式,但希望清除一些单元格
- (谷歌API)单元格引用中的动态工作表和工作表创建脚本的名称
- 谷歌电子表格将一些值存储在其他单元格中
- 检查日期并使用谷歌脚本编辑谷歌表格中的相邻单元格
- 从谷歌电子表格检索单元格日期到HTML服务
- 谷歌地理图表点击设置单元格不起作用
- 如何使用谷歌可视化API来检索谷歌电子表格单元格值
- 谷歌表格循环搜索字符串并清除相邻单元格