我在一个工作表中有一个包含大量数据的电子表格。为了减少混乱,我编写了一个函数来查找日期早于当前日期的行,将它们复制到工作表名称Backup并从当前工作表中删除该条目。
我的fxn如下所示。当我调用它时,它会复制大约一半的数据。在我清空我的工作表之前,我必须运行它几次。当它发生时,它不会将最后一个值写入目标工作表。
我很感激能提高我的功能。
function movePros() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var TZ = CalendarApp.openByName(ss.getSheetByName("Templates").getRange("E1").getValue()).getTimeZone();
var date = Utilities.formatDate(new Date(), TZ, "MM/dd/yyyy HH:mm:ss");
var dataSheet = ss.getSheetByName("Put your events here");
var targetSheet = ss.getSheetByName("Archive");
var dataRange = dataSheet.getRange(2, 1, dataSheet.getMaxRows(), dataSheet.getMaxColumns());
var count = 0;
// Create one JavaScript object per row of data.
objects = getRowsData(dataSheet, dataRange);
var res = objects.length;
for (var i = 0; i < objects.length; ++i) {
var rowData = objects[i];
var tmp = Utilities.formatDate(rowData.endDate, TZ, "MM/dd/yyyy HH:mm:ss");
var last_row = targetSheet.getLastRow();
targetSheet.insertRowAfter(last_row);
var target_range = targetSheet.getRange("A"+(last_row+1)+":H"+(last_row+1));
//Browser.msgBox("The length is " + rowData.eventId.length );
if ((tmp.valueOf() < date.valueOf() ) && (tmp.valueOf().length > 3)){
//Browser.msgBox(rowData.eventId);
var source_range = dataSheet.getRange("A"+(i+2)+":H"+(i+2));
source_range.copyTo(target_range);
count++;
dataSheet.deleteRow(i+2);
SpreadsheetApp.flush();
}
}
ss.toast(count + " rows copied", "Feedback", 5);
}
答案 0 :(得分:2)
function movePros() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var TZ = CalendarApp.openByName(ss.getSheetByName("Templates").getRange("E1").getValue()).getTimeZone();
var date = Utilities.formatDate(new Date(), TZ, "MM/dd/yyyy HH:mm:ss");
var dataSheet = ss.getSheetByName("Put your events here");
var targetSheet = ss.getSheetByName("Archive");
var count = 0;
var orig_last_row = targetSheet.getLastRow(); //we use this as our reference
//dataSheet.getRange(row, column, numRows, numColumns)
var header_data = dataSheet.getRange(1, 1, 1, dataSheet.getMaxColumns()).getValues();
var data = dataSheet.getRange(2, 1, dataSheet.getMaxRows(), dataSheet.getMaxColumns()).getValues();
var srcArray = new Array();
var dstArray = new Array();
var headerArray = new Array();
//get the header into our array, this line contains our column headers
var buff = header_data[0];
headerArray.push(buff);
for (i in data){
var row = data[i];
try {
var tmp = Utilities.formatDate(row[5], TZ, "MM/dd/yyyy HH:mm:ss");
if(tmp.valueOf() < date.valueOf()){
srcArray.push(row);
count++;
}else{
dstArray.push(row);
}
}
catch(err){
//Logger.log("reached the end of the array");
}
}
dataSheet.clearContents();
if(srcArray.length > 0){
targetSheet.getRange(orig_last_row+1, 1, srcArray.length, srcArray[0].length).setValues(srcArray);
}
if(dstArray.length > 0){
dataSheet.getRange(1, 1, headerArray.length, headerArray[0].length).setValues(headerArray);
dataSheet.getRange(2, 1, dstArray.length, dstArray[0].length).setValues(dstArray);
}
ss.toast(count + " rows copied", "Feedback", 5);
}