"/**
 * @OnlyCurrentDoc
 */

function getHistoricalPerformance(){
  
  var ss = SpreadsheetApp.getActiveSpreadsheet();
   
  var userPerformanceDates = ss.getRange('Dashboard!C53:C54').getValues();
  var monthsData = ss.getRange('Monthly Performance!G30:G31').getValues();
  var numMonths = monthsData[0][0];
  var numMonthsCompleted = monthsData[1][0];
  
  var currentDatesArray = [[]];
  currentDatesArray = ss.getRange("Monthly Performance!K"+(numMonthsCompleted+3)+":L"+(numMonths+2)).getDisplayValues();
  
  //set account filter menu to show All Accounts
  ss.getRange('Dashboard!C4').setValue('All');
  SpreadsheetApp.flush();
  
  //grab money-weighted return for the full date range
  var totalDateRange = ss.getRange('Monthly Performance!G26:G27').getValues();
  ss.getRange('Dashboard!C53:C54').setValues(totalDateRange);          
  SpreadsheetApp.flush();
  ss.getRange('Dashboard!I76').copyTo(ss.getRange("Monthly Performance!D153"), {contentsOnly: true});    

  //loop through months from start to end
  for(i=numMonthsCompleted; i<numMonths; i++){
    
    var currentDatesTransposed = [[]];
    currentDatesTransposed = [[currentDatesArray[i-numMonthsCompleted][0]],[currentDatesArray[i-numMonthsCompleted][1]]];
                                   
    ss.getRange('Dashboard!C53:C54').setValues(currentDatesTransposed);
            
    SpreadsheetApp.flush();
    
    var currentOutputRange = "N"+(i+3)+":T"+(i+3);

    //New code added in June 2021 -- force spreadsheet to delay / recalc until errors are gone 
    var errorCheck = ss.getRange('Dashboard!J76').getValues();

    while(errorCheck == "Error"){
      var a1Cell = ss.getRange('Calculations!A1');
      a1Cell.setFormula("=now()");

      Utilities.sleep(3000);// force a pause
      SpreadsheetApp.flush();
      errorCheck = ss.getRange('Dashboard!J76').getValues();
      console.log("Performance calc error still active.");
    }

    ss.getRange('Dashboard!C76:I76').copyTo(ss.getRange("Monthly Performance!"+currentOutputRange), {contentsOnly: true});    
  }
  
  //reset performance date values back to initial user input
  ss.getRange('Dashboard!C53:C54').setValues(userPerformanceDates);
}

function transpose(a){
  return Object.keys(a[0]).map(function (c) { return a.map(function (r) { return r[c]; }); });
}
"

"/**
 * @OnlyCurrentDoc
 */

function getHistoricalPerformance(){
  
  var ss = SpreadsheetApp.getActiveSpreadsheet();
   
  var userPerformanceDates = ss.getRange('Dashboard!C53:C54').getValues();
  var monthsData = ss.getRange('Monthly Performance!G30:G31').getValues();
  var numMonths = monthsData[0][0];
  var numMonthsCompleted = monthsData[1][0];
  
  var currentDatesArray = [[]];
  currentDatesArray = ss.getRange("Monthly Performance!K"+(numMonthsCompleted+3)+":L"+(numMonths+2)).getDisplayValues();
  
  //set account filter menu to show All Accounts
  ss.getRange('Dashboard!C4').setValue('All');
  SpreadsheetApp.flush();
  
  //grab money-weighted return for the full date range
  var totalDateRange = ss.getRange('Monthly Performance!G26:G27').getValues();
  ss.getRange('Dashboard!C53:C54').setValues(totalDateRange);          
  SpreadsheetApp.flush();
  ss.getRange('Dashboard!I76').copyTo(ss.getRange("Monthly Performance!D153"), {contentsOnly: true});    

  //loop through months from start to end
  for(i=numMonthsCompleted; i<numMonths; i++){
    
    console.log("Starting to work on month #: "+i);

    //grab money-weighted return for the full date range -- redundant code added July 11 2024 to force re-calculate before moving on
    ss.getRange('Dashboard!C53:C54').setValues(totalDateRange);          
    SpreadsheetApp.flush();

    var currentDatesTransposed = [[]];
    currentDatesTransposed = [[currentDatesArray[i-numMonthsCompleted][0]],[currentDatesArray[i-numMonthsCompleted][1]]];
    
    SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Dashboard").getRange("B74").activate();
    ss.getRange('Dashboard!C53:C54').setValues(currentDatesTransposed);
    
    SpreadsheetApp.flush();
    Utilities.sleep(2000); //wait and re-calc before moving on -- added July 11 2024
    SpreadsheetApp.flush();

    //check for error -- if current month starting value does not equal previous month end value, wait for script to recalculate
    var fetchDateLength = 2; //reset to default value
    ss.getRange('Your Portfolio Holdings!AD262').setValue(fetchDateLength); 

    for(var tryCounter=0; tryCounter<10; tryCounter++){
      //SpreadsheetApp.flush();
      var currentMonthStartingValue = ss.getRange('Your Portfolio Holdings!AI8').getValue();
      var previousMonthEndingValue = ss.getRange("Monthly Performance!R"+(i+3-1)).getValue();

      if((i==0 && ss.getRange('Your Portfolio Holdings!AO8').getValue() > 1) || Math.abs(currentMonthStartingValue-previousMonthEndingValue) < 1000){
        console.log("Success -- move to next month");
        break;

      } else {
        fetchDateLength += 1;
        console.log("error: increment the fetch date range to "+fetchDateLength);
        console.log("current values: "+currentMonthStartingValue+" vs. "+previousMonthEndingValue);
        ss.getRange('Your Portfolio Holdings!AD262').setValue(fetchDateLength); //increment the historical date range fetch by 1 day

        //SpreadsheetApp.flush();
        //Utilities.sleep(6000); //wait and re-calc before moving on
        SpreadsheetApp.flush();
      }

    }
    
    //SpreadsheetApp.flush();
    SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Monthly Performance").getRange("T"+(i+3)).activate();
    var currentOutputRange = "N"+(i+3)+":T"+(i+3);
    ss.getRange('Dashboard!C76:I76').copyTo(ss.getRange("Monthly Performance!"+currentOutputRange), {contentsOnly: true});
  }
  
  //reset performance date values back to initial user input
  ss.getRange('Dashboard!C53:C54').setValues(userPerformanceDates);
}

function transpose(a){
  return Object.keys(a[0]).map(function (c) { return a.map(function (r) { return r[c]; }); });
}
"

analyse both the code