我正在尝试创建一个自动存档功能,该功能获取工作表条目的日期,每行的第一列,并将其与当前日期进行比较,如果当前日期为n,则将其移动到存档工作表入境后的几个星期。我在某些行上使用的代码,但在某些行上它返回"源范围的坐标超出了工作表的尺寸。"错误。电子表格的某些行在最后一个活动列中有一个条目,而其他行则留空,因此我不知道这可能是错误源自的位置,但我尝试在没有这些列的情况下运行程序范围,我仍然有一个错误。任何帮助将非常感激! 这是我的功能:
function compareDates(){
//change variable depending on how many weeks you want before archived
var numWeeks = 2;
//if sheet names are different change them here
var formDataName = "Sheet1";
var archiveName = "Archived";
//gets date 2 weeks from current date
var date = new Date()-(3600000*24*7*numWeeks);
//gets sheet that form data is on
var sheetForm = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(formDataName);
//gets row & column numbers from form data sheet
var columnNum = sheetForm.getLastColumn();
var rowNum = sheetForm.getLastRow();
//gets sheet that archive is on
var sheetArchive = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(archiveName);
var rangeForm = sheetForm.getRange(2,1,rowNum,1);
//iterates through rows
for(var i = 2; i<=rowNum; i++){
//gets cell with timestamp
var cell = rangeForm.getCell(i,1).getValue();
//compares timestamp to two weeks value
if(cell.valueOf()<date.valueOf()){
//gets outdated row
var range = sheetForm.getRange(i,1,1,columnNum);
//gets where the row needs to move in archive
var tar = sheetArchive.getRange(sheetArchive.getLastRow()+1, 1, 1, columnNum);
//moves row to archive
range.copyTo(tar);
//deletes row in form data sheet
sheetForm.deleteRow(i);
}
}
sheetArchive.sort(1, false);
}
答案 0 :(得分:0)
试试这个:
function compareDates()
{
var numWeeks = 2;
var formDataName = "Sheet1";
var archiveName = "Archived";
var date = new Date().valueOf()-(numWeeks*7*24*60*60*1000);
var sheetForm = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(formDataName);
var columnNum = sheetForm.getLastColumn();
var rowNum = sheetForm.getLastRow();
var sheetArchive = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(archiveName);
var rangeForm = sheetForm.getRange(2,1,rowNum-1,1);
var vA=rangeForm.getValues();
for(var i=vA.length-1;i>=0;i--)
{
if(new Date(vA[i][0]).valueOf()<date.valueOf())
{
var range = sheetForm.getRange(i+2,1,1,columnNum);
var tar = sheetArchive.getRange(sheetArchive.getLastRow()+1, 1, 1, columnNum);
range.copyTo(tar);
sheetForm.deleteRow(i + 2);
}
}
sheetArchive.sort(1, false);
}