简体   繁体   English

谷歌表格中的 Javascript 清理

[英]Javascript Cleanup in Google Sheets

I have the below code that works the way as intended (3 functions total).我有以下代码按预期方式工作(总共 3 个函数)。 I am looking for help in how to write better code.我正在寻求如何编写更好的代码的帮助。 I'm thinking there has to be a more efficient or cleaner way to write this code.我认为必须有一种更有效或更清洁的方式来编写此代码。 In particular, the way I wrote the date code I am confused how it is working, just know that it is.特别是我编写日期代码的方式,我很困惑它是如何工作的,只知道它是。 Please and thank you for any help I can get on this!请并感谢您在这方面提供的任何帮助!

The idea was developing a function that could determine what a delay of revenue timing could look like, given the seasonality of the business.考虑到业务的季节性,这个想法是开发一个 function 可以确定收入时间延迟的情况。 The function pulls in a months value (original end date - new end date). function 提取月份值(原始结束日期 - 新结束日期)。 Then the function will begin to sum the percentages based on the month following the current end month and until the difference in the dates is reached.然后 function 将开始根据当前结束月份之后的月份计算百分比,直到达到日期差异。 Then this percentage value is taken to get us the impact in delayed timing.然后取这个百分比值来得到延迟时间的影响。

IE supposed to start January 2021, but had to move until May 2021. This causes a delay of 4 months. IE 原定于 2021 年 1 月开始,但不得不移动到 2021 年 5 月。这导致延迟了 4 个月。 Starting from February, sum the percentages for the following 4 months.从 2 月开始,将接下来 4 个月的百分比相加。

Google Sheet: Google Sheets Example Google 表格: Google 表格示例

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;
}

This can still be optimized further.这仍然可以进一步优化。 I just used loops and arrays to shorten the repeated lines as it is the quickest way to achieve your goal (which is to make the script cleaner/shorter) without changing the algorithm you applied.我只是使用循环和 arrays 来缩短重复的行,因为这是在不更改您应用的算法的情况下实现目标(使脚本更简洁/更短)的最快方法。

Script:脚本:

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 newEndDate = new Date(endDate.setMonth(endDate.getMonth() + 1))
  var curMth = new Date(endDate.setMonth(newEndDate.getMonth()))

  // array to store your values;
  var mth = [];

  for (i = 10; i > 0; i--) {
    // imitated the your variable naming to the indexing of the array
    mth[i] = new Date(curMth.setMonth(curMth.getMonth() + 1)).getMonth() + 1;
  }

  if (monthNum >= 12) //if number of months delay is >=12, forces 100% of cost
    return 1;

  else if (monthNum >= 1) {
    var negVal = sh.getRange(mthCol(endDate.getMonth() + 1) + curRow).getValue();
    for (i = monthNum - 1; i >= 1; i--) {
      // always start at 10, then go down as loop deepens
      negVal += sh.getRange(mthCol(mth[11 - i]) + curRow).getValue();
    }
    return negVal;
  }
}

This yielded to the same output you had in your sample file.这产生了与示例文件中相同的 output。

Output: Output:

输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM