Tony
Tony

Reputation: 21

Google Sheet script to set custom number formatting based on cell value

I want to format a cell to a custom number format based on a the value of another cell in the same row. For example in range A1:U1000 I want to format cells J1 and K1 to the same custom number format based on the text value of cell G1. I need to propagate this down a couple thousand rows. I edited a script I found that allows me to set the custom number format of the cell I currently have selected, however I want to have it run when ever values in column G are edited. I am having trouble merging it into an onEdit() function.

function testV(){
  setNumberFormat('Volume');
}

function testM(){
  setNumberFormat('Mass');
}

function setNumberFormat(format) {
  var range = SpreadsheetApp.getActiveRange();
  var numberFormat = '';
  try {
    switch (format){
      case 'Volume':
        numberFormat = '##0.00,# in³';
        break;
      case 'Mass':
        numberFormat = '##0.00,# g';
        break;
    }
    range.setNumberFormat(numberFormat);
  } catch (e){
    throw new Error('There was an error: ' + e);
  }
}

Upvotes: 2

Views: 2344

Answers (2)

mitch-NZ
mitch-NZ

Reputation: 328

I created a trigger for a similar post which can be reused here. Try this...

function onEdit(e) {
  // set the range to monitor for edits 
  var editRange = {
    top : 5, bottom : 20,                                                     // row numbers in search range
    left : 7, right : 7                                                       // column numbers in search range
  };
  var thisRow = e.range.getRow();                                             // find the row number that was changed
    if(thisRow < editRange.top || thisRow > editRange.bottom) return;         // exit if not in these rows
  var thisColumn = e.range.getColumn();                                       // find the column number that was edited
    if(thisColumn < editRange.left || thisColumn > editRange.right) return;   // exit if not in these columns

  var shtIn = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("stock");  // set the sheet for the cell that will be formatted 
  var numberFormat = '';
  switch(e.value) {
    case 'Mass':
      numberFormat = '##0.00,# g';
      break;
    case 'Volume':
      numberFormat = '##0.00,# in³'
      break;
  }
  shtIn.getRange(thisRow, 10).setNumberFormat(numberFormat);
  shtIn.getRange(thisRow, 11).setNumberFormat(numberFormat);
}

Here's a link to a sample sheet where it hopefully works as you'd expect.

Upvotes: 1

Tony
Tony

Reputation: 21

I was able to build on the original code to achieve the desired outcome. It does work for me, however it doesn't seem very efficient. The cells I'm formatting take a while to update.

var app = SpreadsheetApp;
var activeSheet = app.getActiveSpreadsheet().getActiveSheet();
var someCell = activeSheet.getActiveRange().getValue();
var someRow = activeSheet.getActiveRange().getRowIndex();
var someColumn = activeSheet.getActiveRange().getColumn();

function onEdit() {
  
  if (someCell == 'Machine 1' && someColumn == '7'){
    setNumberFormat('Mass');
  }
  else if (someCell == 'Machine 2' && someColumn == '7') {
    setNumberFormat('Mass');
  }
  else if (someCell == 'Machine 3' && someColumn == '7') {
    setNumberFormat('Mass');
  }
  else if (someCell == 'Machine 4' && someColumn == '7') {
    setNumberFormat('Volume');
  }
  else if (someCell == 'Machine 5' && someColumn == '7') {
    setNumberFormat('Sheet');
  }
  else if (someCell == 'CAD' && someColumn == '7') {
    setNumberFormat('N/A');
  }
  else if (someCell == '' && someColumn == '7'){
    setNumberFormat('Blank')
  }
}

function setNumberFormat(format) {
  var rangeMaterial = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(someRow,10);
  var rangeSupport = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(someRow,11);
  var numberFormat = '';
  try {
    switch (format){
      case 'Volume':
        numberFormat = '##0.00,# in³';
        break;
      case 'Mass':
        numberFormat = '##0.00,# g';
        break;
      case 'Sheet':
        numberFormat = '##,# "sheets"';
        break;
      case 'N/A':
        numberFormat = '##,# "N/A"';
        break;
      case 'Blank':
        numberFormat = '##,#';
        break;
    }
    rangeMaterial.setNumberFormat(numberFormat);
    rangeSupport.setNumberFormat(numberFormat);
    
  } catch (e){
    throw new Error('There was an error: ' + e);
  }
}

Upvotes: 0

Related Questions