我正在尝试使此脚本运行得更快。
function refreshWeeklyReport() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var weeklyReportSheet = ss.getSheetByName("Weekly Report");
var oneDayAgoReport = initializeDaysAgoReportObject(3,1,1);
var twoDaysAgoReport = initializeDaysAgoReportObject(11,1,2);
var threeDaysAgoReport = initializeDaysAgoReportObject(19,1,3);
var fourDaysAgoReport = initializeDaysAgoReportObject(27,1,4);
var fiveDaysAgoReport = initializeDaysAgoReportObject(35,1,5);
var sixDaysAgoReport = initializeDaysAgoReportObject(43,1,6);
var sevenDaysAgoReport = initializeDaysAgoReportObject(51,1,7);
var reports = [oneDayAgoReport, twoDaysAgoReport, threeDaysAgoReport, fourDaysAgoReport, fiveDaysAgoReport, sixDaysAgoReport, sevenDaysAgoReport];
reports.forEach(function(report) {
// Assign random colors
var colorDiff = randomColor(150);
var r = colorDiff.r;
var g = colorDiff.g;
var b = colorDiff.b;
var summary = getSumsForDates(report.date, addDays(report.date,1), true, r,g,b, false);
Logger.log('Summary' + JSON.stringify(summary));
Logger.log('Report' + JSON.stringify(report));
report.linearFeetCell.setValue(summary.linearFeet);
report.piecesCell.setValue(summary.pieces);
report.boardFootageCell.setValue(summary.boardFootage);
report.trussesCell.setValue(summary.trusses);
report.salesDollarsCell.setValue(summary.salesDollars);
report.setupsCell.setValue(summary.setups);
// Randomize colors here
report.dayCell.setBackgroundRGB(r, g, b);
});
}
这是另一个要调用的函数:
function initializeDaysAgoReportObject(startRow, startColumn, numberOfDaysAgo)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var weeklyReportSheet = ss.getSheetByName("Weekly Report");
var daysAgoReport = {}; // Creates an object
// Variables
daysAgoReport.startRow = startRow;
daysAgoReport.startColumn = startColumn;
daysAgoReport.date = minusDays(new Date(), numberOfDaysAgo);
daysAgoReport.date.setHours(0);
daysAgoReport.day = days[daysAgoReport.date.getDay()];
daysAgoReport.dateCell = weeklyReportSheet.getRange(startRow,startColumn + 1);
daysAgoReport.dayCell = weeklyReportSheet.getRange(startRow,startColumn);
daysAgoReport.linearFeetCell = weeklyReportSheet.getRange(startRow+1,startColumn+1);
daysAgoReport.piecesCell = weeklyReportSheet.getRange(startRow+3,startColumn+1);
daysAgoReport.boardFootageCell = weeklyReportSheet.getRange(startRow+5,startColumn+1);
daysAgoReport.trussesCell = weeklyReportSheet.getRange(startRow+1,startColumn+7);
daysAgoReport.salesDollarsCell = weeklyReportSheet.getRange(startRow+1,startColumn+4);
daysAgoReport.setupsCell = weeklyReportSheet.getRange(startRow+2,startColumn+7);
daysAgoReport.nonOverTimeLaborCostCell = weeklyReportSheet.getRange(startRow+4,startColumn+4);
daysAgoReport.overTimePremiumCostCell = weeklyReportSheet.getRange(startRow+6,startColumn+4);
daysAgoReport.nonOverTimeLaborHoursCell = weeklyReportSheet.getRange(startRow+2,startColumn+10);
daysAgoReport.overTimeLaborHoursCell = weeklyReportSheet.getRange(startRow+4,startColumn+10);
var displayDate = addDays(daysAgoReport.date,1);
daysAgoReport.dayCell.setValue(daysAgoReport.day);
daysAgoReport.dateCell.setValue(displayDate);
//set labor cells
var laborData = getTotalsFromLabor(daysAgoReport.date);
daysAgoReport.nonOverTimeLaborCostCell.setValue(laborData.regularLaborCost);
daysAgoReport.overTimePremiumCostCell.setValue(laborData.overTimePremium);
daysAgoReport.nonOverTimeLaborHoursCell.setValue(laborData.regularHours);
daysAgoReport.overTimeLaborHoursCell.setValue(laborData.overtimeHours);
return daysAgoReport;
}
此函数进行计算:
function getSumsForDates(dateStart, dateEnd, highlightActiveRows, r,g,b, clearInActiveRows)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var reportSheet = ss.getSheetByName("Custom Report");
var summary = {};
summary.linearFeet = 0;
summary.pieces = 0;
summary.boardFootage =0;
summary.trusses = 0;
summary.salesDollars = 0;
summary.setups = 0;
var summaryOffset = 15;
// If Customeer Report data exceeds 3000, TypeError will occur.
var dateColumn = reportSheet.getRange(summaryOffset,17,3000,1);
var values = dateColumn.getValues(); // get all data in one call
var currentRow = 0;
while ( values[currentRow][0] != "" ) {
var dateBuilt = new Date(values[currentRow][0]);
var afterStart = dateBuilt.getTime() >= dateStart.getTime();
var beforeEnd = dateBuilt.getTime() <= dateEnd.getTime()
var currentRange = reportSheet.getRange(currentRow + summaryOffset,1,1,17)
var currentValues = currentRange.getValues();
if( afterStart && beforeEnd )
{
var linearFeet = currentValues[0][6];
var pieces = currentValues[0][9];
var boardFootage = currentValues[0][13];
var trusses = currentValues[0][1];
var salesDollars = currentValues[0][15];
summary.linearFeet += linearFeet;
summary.pieces += pieces;
summary.boardFootage += boardFootage;
summary.trusses += trusses;
summary.salesDollars += salesDollars;
summary.setups++
if(highlightActiveRows)
{
currentRange.setBackgroundRGB(r,g,b);
}
}
else
{
if(clearInActiveRows)
{
currentRange.setBackgroundRGB(255,255,255);
}
}
currentRow++;
}
return summary;
}
我相信大多数优化应该在getSumsForDates函数中进行,因为它正在进行所有读取和计算。除了遍历每一行之外,还有没有更快速的方法?