【发布时间】:2021-12-14 22:33:46
【问题描述】:
我的以下代码按预期方式工作(总共 3 个函数)。我正在寻求如何编写更好的代码的帮助。我认为必须有一种更有效或更清洁的方式来编写此代码。特别是我编写日期代码的方式,我很困惑它是如何工作的,只知道它是。请并感谢您为我提供的任何帮助!
考虑到业务的季节性,我们的想法是开发一种功能,该功能可以确定收入时间延迟的情况。该函数提取月份值(原始结束日期 - 新结束日期)。然后该函数将根据当前结束月份之后的月份开始对百分比进行求和,直到达到日期的差异。然后取这个百分比值来得到延迟时间的影响。
IE 原定于 2021 年 1 月开始,但不得不移动到 2021 年 5 月。这导致延迟了 4 个月。从 2 月开始,将接下来 4 个月的百分比相加。
function rateChange(monthNum) {//main program to calculate the percent of lost revenue, based on delay in timing
var sh = SpreadsheetApp.getActiveSpreadsheet()
var sheet = SpreadsheetApp.getActiveSheet().getActiveCell()
var sheet2 = sh.getSheetByName("Rate Change")
var curRow = sheet.getRow()
var endDate = new Date(sheet2.getRange("F" + curRow).getValue());
var endMth = endDate.getMonth();
var newEndDate = new Date(endDate.setMonth(endDate.getMonth()+1))
var curMth = new Date(endDate.setMonth(newEndDate.getMonth()))
var mth10Dt = new Date(curMth.setMonth(curMth.getMonth()+1))
var mth10 = mth10Dt.getMonth()+1;
var mth9Dt = new Date(curMth.setMonth(curMth.getMonth()+1))
var mth9 = mth9Dt.getMonth()+1;
var mth8Dt = new Date(curMth.setMonth(curMth.getMonth()+1))
var mth8 = mth8Dt.getMonth()+1;
var mth7Dt = new Date(curMth.setMonth(curMth.getMonth()+1))
var mth7 = mth7Dt.getMonth()+1;
var mth6Dt = new Date(curMth.setMonth(curMth.getMonth()+1))
var mth6 = mth6Dt.getMonth()+1;
var mth5Dt = new Date(curMth.setMonth(curMth.getMonth()+1))
var mth5 = mth5Dt.getMonth()+1;
var mth4Dt = new Date(curMth.setMonth(curMth.getMonth()+1))
var mth4 = mth4Dt.getMonth()+1;
var mth3Dt = new Date(curMth.setMonth(curMth.getMonth()+1))
var mth3 = mth3Dt.getMonth()+1;
var mth2Dt = new Date(curMth.setMonth(curMth.getMonth()+1))
var mth2 = mth2Dt.getMonth()+1;
var mth1Dt = new Date(curMth.setMonth(curMth.getMonth()+1))
var mth1 = mth1Dt.getMonth()+1;
if(monthNum >= 12){//if number of months delay is >=12, forces 100% of cost
var negVal = 1
return negVal;
}
else if (monthNum == 11){//begin summation of annual percentages, given the number of months between original planned date and new planned date
var negVal = sh.getRange(mthCol(endDate.getMonth()+1) + curRow).getValue() + sh.getRange(mthCol(mth10) + curRow).getValue() + sh.getRange(mthCol(mth9) + curRow).getValue() + sh.getRange(mthCol(mth8) + curRow).getValue()+sh.getRange(mthCol(mth7) + curRow).getValue()+sh.getRange(mthCol(mth6) + curRow).getValue() + sh.getRange(mthCol(mth5) + curRow).getValue()+sh.getRange(mthCol(mth4) + curRow).getValue()+sh.getRange(mthCol(mth3) + curRow).getValue() + sh.getRange(mthCol(mth2) + curRow).getValue() + sh.getRange(mthCol(mth1) + curRow).getValue();
return negVal;
}
else if(monthNum == 10){
var negVal = sh.getRange(mthCol(endDate.getMonth()+1) + curRow).getValue() + sh.getRange(mthCol(mth10) + curRow).getValue() + sh.getRange(mthCol(mth9) + curRow).getValue() + sh.getRange(mthCol(mth8) + curRow).getValue()+sh.getRange(mthCol(mth7) + curRow).getValue()+sh.getRange(mthCol(mth6) + curRow).getValue() + sh.getRange(mthCol(mth5) + curRow).getValue()+sh.getRange(mthCol(mth4) + curRow).getValue()+sh.getRange(mthCol(mth3) + curRow).getValue() + sh.getRange(mthCol(mth2) + curRow).getValue();
return negVal;
}
else if(monthNum == 9){
var negVal = sh.getRange(mthCol(endDate.getMonth()+1) + curRow).getValue() + sh.getRange(mthCol(mth10) + curRow).getValue() + sh.getRange(mthCol(mth9) + curRow).getValue() + sh.getRange(mthCol(mth8) + curRow).getValue()+sh.getRange(mthCol(mth7) + curRow).getValue()+sh.getRange(mthCol(mth6) + curRow).getValue() + sh.getRange(mthCol(mth5) + curRow).getValue()+sh.getRange(mthCol(mth4) + curRow).getValue()+sh.getRange(mthCol(mth3) + curRow).getValue();
return negVal;
}
else if(monthNum == 8){
var negVal = sh.getRange(mthCol(endDate.getMonth()+1) + curRow).getValue() + sh.getRange(mthCol(mth10) + curRow).getValue() + sh.getRange(mthCol(mth9) + curRow).getValue() + sh.getRange(mthCol(mth8) + curRow).getValue()+sh.getRange(mthCol(mth7) + curRow).getValue()+sh.getRange(mthCol(mth6) + curRow).getValue() + sh.getRange(mthCol(mth5) + curRow).getValue()+sh.getRange(mthCol(mth4) + curRow).getValue();
return negVal;
}
else if(monthNum == 7){
var negVal = sh.getRange(mthCol(endDate.getMonth()+1) + curRow).getValue() + sh.getRange(mthCol(mth10) + curRow).getValue() + sh.getRange(mthCol(mth9) + curRow).getValue() + sh.getRange(mthCol(mth8) + curRow).getValue()+sh.getRange(mthCol(mth7) + curRow).getValue()+sh.getRange(mthCol(mth6) + curRow).getValue() + sh.getRange(mthCol(mth5) + curRow).getValue();
return negVal;
}
else if(monthNum == 6){
var negVal = sh.getRange(mthCol(endDate.getMonth()+1) + curRow).getValue() + sh.getRange(mthCol(mth10) + curRow).getValue() + sh.getRange(mthCol(mth9) + curRow).getValue() + sh.getRange(mthCol(mth8) + curRow).getValue()+sh.getRange(mthCol(mth7) + curRow).getValue()+sh.getRange(mthCol(mth6) + curRow).getValue();
return negVal;
}
else if(monthNum == 5){
var negVal = sh.getRange(mthCol(endDate.getMonth()+1) + curRow).getValue() + sh.getRange(mthCol(mth10) + curRow).getValue() + sh.getRange(mthCol(mth9) + curRow).getValue() + sh.getRange(mthCol(mth8) + curRow).getValue()+sh.getRange(mthCol(mth7) + curRow).getValue();
return negVal;
}
else if(monthNum == 4){
var negVal = sh.getRange(mthCol(endDate.getMonth()+1) + curRow).getValue() + sh.getRange(mthCol(mth10) + curRow).getValue() + sh.getRange(mthCol(mth9) + curRow).getValue() + sh.getRange(mthCol(mth8) + curRow).getValue();
return negVal;
}
else if(monthNum == 3){
var negVal = sh.getRange(mthCol(endDate.getMonth()+1) + curRow).getValue() + sh.getRange(mthCol(mth10) + curRow).getValue() + sh.getRange(mthCol(mth9) + curRow).getValue();
return negVal;
}
else if(monthNum == 2){
var negVal = sh.getRange(mthCol(endDate.getMonth()+1) + curRow).getValue() + sh.getRange(mthCol(mth10) + curRow).getValue();
return negVal;
}
else if(monthNum == 1){
var negVal = sh.getRange(mthCol(endDate.getMonth()+1) + curRow).getValue();
return negVal;
}
}
function mthCol(curMth) {//Determines start month of calculation and returns letter of the column
var sh = SpreadsheetApp.getActiveSpreadsheet()
var sheet = sh.getSheetByName("Rate Change")
for(var i = 11; i<23; i++){
if(sheet.getRange(1, i).getValue() == curMth){
var colMth = columnToLetter(i);
return colMth;
}
}
}
function columnToLetter(column){//Gets column letter
var temp, letter = '';
while (column >0){
temp = (column - 1) % 26;
letter = String.fromCharCode(temp + 65) + letter;
column = (column - temp -1) / 26;
}
return letter;
}
【问题讨论】:
-
我必须为我糟糕的英语水平道歉。不幸的是,我无法理解你的问题。我可以问你问题的细节吗?顺便说一句,我无法打开您的示例电子表格。你能确认一下吗?
-
我更新了链接。两种方式都对我有用。我正在寻找一种改进代码的方法,以便它运行得更快,流动性更好。
-
请将该文件提供给公共@GTL。我也无法访问该文件。
-
抱歉,此链接现在对任何知道该链接的人开放
-
感谢您的回复。现在我注意到已经发布了答案。我想尊重现有的答案。
标签: javascript google-apps-script google-sheets