这是一些代码,它应该可以解决问题。
function generateReccuringExpenses(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var inputSheet = ss.getSheetByName("Input Table");
var outputSheet = ss.getSheetByName("Output Table");
var endYear = 2020;
// We considere your data to be on a sheet with a header, starting on A1 cell
var data = inputSheet.getRange(2, 1, inputSheet.getLastRow()-1, inputSheet.getLastColumn()).getValues();
for(var i = 0; i < data.length; i++){
var row = data[i];
if(row[3] > 0){
var elements = new Array();
var d = new Date(row[0]);
while(d.getFullYear() < endYear){
var test = d.getFullYear();
var month = d.getMonth() +1;
var dateString = month+"/"+d.getDate()+"/"+d.getFullYear();
var newRow = [dateString, row[1],row[2]];
elements.push(newRow);
var n = d.getMonth() + row[3];
if(n > 11){
d.setMonth(n -11);
d.setYear(d.getYear() + 1);
}
else{
d.setMonth(n);
}
}
var outputRange = outputSheet.getRange(outputSheet.getLastRow()+1, 1, elements.length, outputSheet.getLastColumn());
outputRange.setValues(elements);
}
}
var range = outputSheet.getRange(2, 1, outputSheet.getLastRow()+1, outputSheet.getLastColumn());
range.sort(1); // Sort by date (column 1)
}
您可以使用 Range.sort() 方法或电子表格工具对结果进行排序。