简体   繁体   中英

Google Apps Script custom function for sheets - range not found

I have an issue that's likely due to my lacking understanding on how the getRange() function works.

I wrote two small functions to use in a Google Sheet, but I can't get cell references to work properly as input. Here is the code:

function getCellRGB(input, color) {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];

  var cell = sheet.getRange(input);
  var hex = cell.getBackground();
  hex = hex.replace('#','');

  if (color == "r") return parseInt(hex.substring(0,2), 16);
  else if (color == "g") return parseInt(hex.substring(2,4), 16);
  else if (color == "b") return parseInt(hex.substring(4,6), 16);
  else return null;

}

This only works if I input like this:

getCellRGB("A1", "r")

if I attempt to use a normal cell reference like they are used in other functions:

getCellRGB(A1, "r")

I get the error "Range not found (line 6)"

The second function colors a cell from a R, G and B value in one cell each:

function setCellColorFromRGB(red,green,blue) {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];

  var r = sheet.getRange(red);
  var g = sheet.getRange(green);
  var b = sheet.getRange(blue);

  cell.setBackgroundRGB(r, g, b)

}

I get the same error for this one, also line 6.

Change this line

 var cell = sheet.getRange(input);

to

 var cell = sheet.getRange('"' + input + '"');

and you should be putting quotes around the passed value. You should be able to do the same for the other lines.

The getRange() method accepts several different types of arguments. For one cell you have two choices: a string in the a1 notation or two integers counting starting at 1.

So:

//these are valid and will get the top left most cell
var cell = sheet.getRange('A1');
var cell = sheet.getRange(1,1);

//this is not valid
var willNotWork = sheet.getRange(A1);

After you get the cell, you also need to call .getValue(); to get the contents of the cell.

Thanks everyone for your efforts, I've actually managed myself to find the solutions to them. There were quite a few more mistakes than I thought.

First of all, the code for my first function:

 function getCellRGB(sheet, row, col, color) { var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheetByName(sheet); var cell = sheet.getRange(row, col); var hex = cell.getBackground(); hex = hex.replace('#',''); if (color == "r") return parseInt(hex.substring(0,2), 16); else if (color == "g") return parseInt(hex.substring(2,4), 16); else if (color == "b") return parseInt(hex.substring(4,6), 16); else return null; } 

  1. Issue, input. It turns out when you call a function in sheets like so:

    =getCellRGB(Sheet1, A1, A1, "r")

A1 and B1 actually return the values of those cells, not the reference to the cells themselves. In fact, it doesn't look like there is a way to easily input a cell reference into a function. I solved this by doing the input like so:

=getCellRGB(Sheet1 ROW(A1), COL(A1), "r")

Which is more complicated but works and even allows me to "formula paste" in Sheets.

  1. Input the sheet It's necessary to input the sheet where the cell to take the RGB from is located as I wanted it to be possible for it to be on a different sheet in the spreadsheet, not just the same sheet.

  2. issue the getRange() function This one took a while since I thought the only way to use it is with a cell reference as a String - getRange("A1"). While that is one way of doing it, it's also possible to input a row and column instead, to input which there are the defaul ROW() and COL() functions. That worked.

So this code works and I've used it to output the correct variables, but the issue is it turns out I was actually looking for a constantly running script since the RGB values were supposed to be read and output constantly instead of just once on entering the function. That made a different code entirely necessary which is really a different topic but since it may help people having the same realization, here it is:

 function onEdit(e) { var ss = SpreadsheetApp.getActiveSpreadsheet(); var edit = ss.getSheetByName("Edit"); var calculation = ss.getSheetByName("Calculation"); var listenRange = edit.getRange("M2:M"); var applyRange = calculation.getRange("D2:F"); if (e.source.getSheetName() != "Edit") { //ss.toast('nopeSheet' + ":" + e.source.getSheetName()); return; } var row = e.range.getRow(); if (row < 2) { // ss.toast('nopeRow' + ":" + row); return; } var col = e.range.getColumn(); if (col < 10 || col > 10) { // ss.toast('nopeCol' + ":" + col); return; } var row = e.range.getRow(); var col = e.range.getColumn(); var cell = edit.getRange(row,col); var hex = cell.getBackground(); hex = hex.replace('#',''); var red = parseInt(hex.substring(0,2), 16); var green = parseInt(hex.substring(2,4), 16); var blue = parseInt(hex.substring(4,6), 16); var redCell = calculation.getRange("D" + row); redCell.setValue(red); var greenCell = calculation.getRange("E" + row); greenCell.setValue(green); var blueCell = calculation.getRange("F" + row); blueCell.setValue(blue); } 

There's some commented out debug code in there but otherwise this works with the exception of not triggering properly because the change of a background color apparently doesn't trigger onEdit. A fatal flaw but again, topic for another question and for everything but the color change this will work.

I've also hardcoded the sheet references and ranges but since I'm going to only use it for this sheet specifically that should be no issue.

Secondly regarding my other original piece of code - setting the background color of a cell based on RGB values in different cells. This turned out to be a major issue since changing a cells background color based on values in another cell is apparently not allowed for a function. I've managed to get around it with these two functions:

 function rgbToHex(r,g,b) { var r = parseInt(r); var g = parseInt(g); var b = parseInt(b); if (g !== undefined) return Number(0x1000000 + r*0x10000 + g*0x100 + b).toString(16).substring(1); else return Number(0x1000000 + r[0]*0x10000 + r[1]*0x100 + r[2]).toString(16).substring(1); } 
source: How to convert decimal to hex in JavaScript?

 function setBgColor() { var s = SpreadsheetApp.getActive().getSheetByName("Calculation"); var targetrange = s.getRange('L2:L').setBackgrounds(s.getRange('K2:K').getValues()); } 
source: https://productforums.google.com/forum/#!topic/docs/88lWZY7WDZI

Since I only needed this as a one-time function, this worked well but it could possibly be converted to work differently as well.

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