简体   繁体   中英

Javascript Cleanup in Google Sheets

I have the below code that works the way as intended (3 functions total). 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. The function pulls in a months value (original end date - new end date). 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. 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. Starting from February, sum the percentages for the following 4 months.

Google Sheet: Google Sheets Example

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.

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:

输出

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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