简体   繁体   English

表格的Google Apps脚本自定义功能-找不到范围

[英]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. 我有一个问题,可能是由于我对getRange()函数的工作方式缺乏了解。

I wrote two small functions to use in a Google Sheet, but I can't get cell references to work properly as input. 我写了两个小的函数在Google表格中使用,但无法获取单元格引用作为输入正常工作。 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)" 我收到错误消息“找不到范围(第6行)”

The second function colors a cell from a R, G and B value in one cell each: 第二个函数分别从一个单元格中的R,G和B值为一个单元格着色:

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. 对于这一行,我也得到了同样的错误,也就是第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. getRange()方法接受几种不同类型的参数。 For one cell you have two choices: a string in the a1 notation or two integers counting starting at 1. 对于一个单元格,您有两种选择:a1表示法中的字符串或从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(); 获取单元格后,还需要调用.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") = getCellRGB(Sheet1,A1,A1,“ r”)

A1 and B1 actually return the values of those cells, not the reference to the cells themselves. A1和B1实际上返回那些单元格的值,而不是对单元格本身的引用。 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"). 发出getRange()函数由于我认为使用它的唯一方法是将单元格引用作为字符串使用-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. 尽管这是一种实现方式,但也可以输入行和列,以输入其中包含默认的ROW()和COL()函数。 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. 因此,此代码有效,并且我已使用它来输出正确的变量,但是问题是,事实证明我实际上是在寻找一个持续运行的脚本,因为应该连续读取和输出RGB值,而不是在输入时一次输出功能。 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. 那里有一些注释掉的调试代码,但是除此之外它不能正常触发,因为背景颜色的改变显然不会触发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. 其次,关于我的另一段原始代码-根据不同单元格中的RGB值设置单元格的背景色。 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? 来源: 如何在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 来源: 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. 由于我只需要将此功能用作一次性功能,因此效果很好,但也可以转换为其他功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM