简体   繁体   中英

Google Sheets:How to update a cell with the range of values in one column, based on the contents of another range of values in different column?

在此处输入图像描述

In cell K18, I want to see the range of values from column F (Velocity), which should be dependent on the range (same rows) from column G (Peak dB). So something like 37-39 or 37 to 39 to be displayed in the cell K18 (or whichever syntax is best to use for pivot tables and/or charts (maybe a low column and a high column is better for making my chart? I don't know).

Column G is set to Conditional Formatting, to colour cells falling between multiple different ranges (-9 to-8, -8 to -7 etc)

The =COUNTIFS function in column J shows the total number of hits/occurences within the given range from G column(Peak dB). (I coloured the cells to match the Conditional Formatting colours of Column G manually).

In the end, the outcome i want, is to bring this info into a chart that neatly overlays/stacks the data from columns I (dB Range), J (No of hits) and K (Velocity Ranges), together.

Given the sample data I'd suggest you to use Google Apps Script

Here's my approach:

function processVelocityRanges() {
  let ss = SpreadsheetApp.getActiveSpreadsheet();
  let sheet = ss.getActiveSheet();
  
  let dataLength = sheet.getDataRange().getValues().length; // Get number of rows
  let velocityRange = sheet.getRange(`F2:F${dataLength}`).getValues(); // Column F
  let peakDBRange = sheet.getRange(`G2:G${dataLength}`).getValues(); // Column G
  let dbRange = sheet.getRange(`I2:I${dataLength}`).getValues(); // Column I

  for(dbRangeIndex = 0; dbRangeIndex < dbRange.length; dbRangeIndex++) {
    let value = dbRange[dbRangeIndex][0];
    if(value) {
      let splitValue = value.split(" to ");
      let lesserValue = Number.parseInt(splitValue[0]);
      let greaterValue = Number.parseInt(splitValue[1]);
      
      // Get the velocity range
      let velocityRangeText = getVelocityRange(lesserValue, greaterValue, velocityRange, peakDBRange);
      if (velocityRangeText) {
        let rowToChange = dbRangeIndex + 2;
        // Change the text in K column
        sheet.getRange(`K${rowToChange}`).setValue(velocityRangeText);
      }
    }
  }
}

function getVelocityRange(lesserValue, greaterValue, velocityRange, peakDBRange) {
  let velocityValues = [];
  for(i = 0; i < peakDBRange.length; i++) {
    if (peakDBRange[i][0] <= greaterValue && peakDBRange[i][0] >= lesserValue) {
      velocityValues.push(velocityRange[i][0]);
    }
  }

  if (velocityValues.length <= 1) {
    return "";
  }

  return `${Math.min(...velocityValues)} to ${Math.max(...velocityValues)}`; // destructuring assignment
}

First of all retrieve the ranges where you have to make comparisons, then using these make a simple search. This code only provides text if there are at least 2 values in order to create a "min TO max" text. Otherwise it just returns an empty string from getVelocityRange function and doesn't set any value on your Sheet.

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