Search code examples
google-apps-scriptgoogle-sheetsnamed-ranges

Google Sheets Script TextFinder FormulaText Doesn't Work with Named Ranges?


I am trying to create a function that modifies formulas in a range that changes bounds over time.

WORKS

var range=ss.getSheetByName('cleaned').getRange("A499:AH540");
sumForm=range.createTextFinder("ss2").matchFormulaText(true);
sumForm.replaceAllWith('ss');

DOESN'T WORK

var range=ss.getRangeByName('cleanedSums');
sumForm=range.createTextFinder("ss2").matchFormulaText(true);
sumForm.replaceAllWith('ss');

Named Range Definition

Yes, I realize the static defined range is slightly different than the named range definition, but the formulae in question appear in columns E, M, V, & AD.

I created a publicly editable version of the sheet: https://docs.google.com/spreadsheets/d/1v_rYBDz6NWmv5JFghsNl2ScAP3u0fq2QHEmZM5ArvG4/edit

The function in question is ss1(), accessible from UI menu Hide (Create SS1). Any insights would be appreciated.

I am not sure how to go about implementing this functionality without using a silly helper cell at the lower bound of the data, which would eventually become inaccurate or iterating over the rows to find the lower bound each time. Neither are particularly appealing and I am struggling to think of why the TextFinder replacement function would not work on named ranges. Sorry if this belongs on /webapps


Solution

  • Your code works as a separate function.

    Most likely your global variables interfered with the script.

    function myFunctionTest() {   
      var ss = SpreadsheetApp.getActiveSpreadsheet();   
      var range=ss.getRangeByName('cleanedSums');   
      sumForm=range.createTextFinder("ss2").matchFormulaText(true);   
      sumForm.replaceAllWith('ss'); 
    }