Google Sheets: INDIRECT() with a Range

后端 未结 3 1224
予麋鹿
予麋鹿 2021-01-24 00:46

This question could be rephrased to, \"Using a programmatically generated range in FILTER()\" depending on the approach one takes to answer it.

EDIT- It seems that I ina

3条回答
  •  迷失自我
    2021-01-24 01:01

    The OP's existing solution is based on Filter command. The challenge is that the column containing "student.grade" is not fixed, however player0 has provided an excellent formula-based solution.

    An alternative might be to make use of a named range. The following code finds "student.grades" in the header (row 1) and re-defines the named range accordingly.

    function so54541923() {
    
      // setup the spreadsheet
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sheetname = "Data Import";
      var sheet = ss.getSheetByName(sheetname);
    
      // define the header row
      var getlastColumn = sheet.getLastColumn();
      var headerRange = sheet.getRange(1, 1, 1, getlastColumn);
      Logger.log("DEBUG: Header range = " + headerRange.getA1Notation()); //DEBUG
    
      // assign a variable for student Grades
      var grades = "student.grade";
    
      // get the headers and find the column containing "student grades"
      var headerData = headerRange.getValues();
      var gradesIndex = headerData[0].indexOf(grades);
      // add one to the index number to account for start=zero
      gradesIndex = gradesIndex + 1;
      Logger.log("DEBUG: gradesIndex = " + gradesIndex); //DEBUG
    
      // convert the column number to a letter 
      // assumes that student.grade will never get past column Z
      var temp, letter = '';
      if (gradesIndex > 0) {
        temp = (gradesIndex - 1) % 26;
        letter = String.fromCharCode(temp + 65) + letter;
        gradesIndex = (gradesIndex - temp - 1) / 26;
      }
      Logger.log("DEBUG: the column is " + letter); //DEBUG
    
      //var newrange = "'" + sheetname + "'!"+letter+":"+letter+";";
      // Logger.log("the new range is "+newrange); 
    
      // get the named ranges
      var namedRanges = ss.getNamedRanges();
      Logger.log("DEBUG: number of ranges: " + namedRanges.length); //DEBUG
    
      // if named range is student grades, then update range
      if (namedRanges.length > 0) {
        for (var i = 0; i < namedRanges.length; i++) {
          var thename = namedRanges[i].getName();
          Logger.log("DEBUG: Loop: i: " + i + ", and the named range is " + thename); //DEBUG
          if (thename = "student.grade") {
    
            // Logger.log("DEBUG: The named range is student.grade");//DEBUG
    
            // set the new range based on the column found earlier
            var nonstringrange = sheet.getRange("'" + sheetname + "'!" + letter + ":" + letter);
            namedRanges[i].setRange(nonstringrange);
            Logger.log("DEBUG: The new range is " + namedRanges[i].getRange().getA1Notation()); //DEBUG
    
          } else {
            Logger.log("DEBUG: The named range is NOT grades"); //DEBUG
          }
        }
      }
    }
    

提交回复
热议问题