简体   繁体   English

Google Apps脚本getRange()范围未找到错误

[英]Google apps script getRange() range not found error

I feel like I'm going about this in all the wrong way. 我觉得我正在以错误的方式来解决这个问题。 I'm trying to automate some of my workload here. 我试图在这里自动化一些工作量。 I'm cleaning up spreadsheets with 4 columns (AE), 2000+ rows. 我正在清理具有4列(AE),超过2000行的电子表格。 Column B contains website URLs, column D contains the URL's business name, generated from another source. B列包含网站URL,D列包含URL的公司名称,该名称是从另一个来源生成的。

Sometimes the tool doesn't grab the name correctly or the name is missing, so it populates the missing entries in column D with "------" (6 hyphens). 有时,该工具无法正确获取名称或名称丢失,因此它在D列中的缺失条目中填充了“ ------”(6个连字符)。 I've been trying to make a function that takes an input cell, checks if the contents of the cell are "------", and if it is the function changes the contents of the input cell to the contents of the cell two columns to the left (which is generally a website url). 我一直在尝试制作一个带有输入单元格的函数,检查该单元格的内容是否为“ ------”,如果是该函数,则将输入单元格的内容更改为单元格左边两列(通常是网站网址)。 This is what I've come up with. 这就是我想出的。

function replaceMissing(input) {
    var sheet = SpreadsheetApp.getActiveSheet();
    //sets active range to the input cell
    var cell = sheet.getRange('"' + input + '"');
    //gets cell to fill input cell
    var urlCell = sheet.getRange(cell.getRow(), cell.getColumn() - 2);
    //gets contents of input cell as String
    var data = cell.getValue(); 
    //gets contents of urlCell as String
    var data2 = cell.getValue();


    //checks if input cell should be replaced
    if (data === "------") { 
      //set current cell's value to the value of the cell 2 columns to the left
      cell.setValue(data2);
    }
}

When I attempt to use my function in my sheet, the cell is returning the error 当我尝试在工作表中使用函数时,单元格返回错误

Error Range not found (line 4).

I'm assuming, based on similar questions people have asked, that this is how you use the A1 notation of the function with an argument. 基于人们提出的类似问题,我假设这是您如何将函数的A1表示法与参数一起使用。 However, that doesn't seem to be the case, so I'm stuck. 但是,事实并非如此,所以我陷入了困境。 I also don't think my solution is very good period. 我也认为我的解决方案不是很好的时期。

1) It's somewhat ambiguous in GAS documentation, but custom functions have quite a few limitations. 1)在GAS文档中有些模糊,但是自定义函数有很多限制。 They are better suited for scenarios where you need to perform a simple calculation and return a string or a number type value to the cell. 它们更适合需要执行简单计算并将字符串或数字类型值返回到单元格的情况。 While custom functions can call some GAS services, this practice is strongly discouraged by Google. 尽管自定义函数可以调用某些GAS服务,但Google强烈建议不要这样做。

If you check the docs for the list of supported services, you'll notice that they support only some 'get' methods for Spreadsheet service, but not 'set' methods https://developers.google.com/apps-script/guides/sheets/functions 如果您在文档中查看受支持的服务列表,则会注意到它们仅支持电子表格服务的某些“获取”方法,而不支持https://developers.google.com/apps-script/guides的 “设置”方法/张/功能

That means you can't call cell.setValue() in the context of a custom function. 这意味着您不能在自定义函数的上下文中调用cell.setValue()。 It makes sense if you think about it - your spreadsheet can contain 1000s of rows, each with its own custom function making multiple calls to the server. 如果您考虑一下,这是有道理的-您的电子表格可以包含数千行,每个行都有自己的自定义函数,可以多次调用服务器。 In JavaScript, every function call creates its own execution context, so things could get ugly very quickly. 在JavaScript中,每个函数调用都会创建自己的执行上下文,因此事情很快就会变得很丑陋。

2) For better performance, use batch operations and don't alternate between read / write actions. 2)为了获得更好的性能,请使用批处理操作,并且不要在读/写操作之间交替。 Instead, read all the data you need for processing into variables and leave the spreadsheet alone. 取而代之的是,将您需要处理的所有数据读入变量,而不必考虑电子表格。 After processing your data, perform a single write action to update values in the target range. 处理完数据后,执行一次写操作以更新目标范围内的值。 There's no need to go cell by cell when you can get the entire range using GAS. 当您可以使用GAS获得整个范围时,就不需要一个接一个的单元了。

Google Apps Script - best practices https://developers.google.com/apps-script/guides/support/best-practices Google Apps脚本-最佳做法https://developers.google.com/apps-script/guides/support/best-practices

Below is a quick code example that runs onOpen and onEdit. 下面是运行onOpen和onEdit的快速代码示例。 If you need more flexibility in terms of when to run the script, look into dynamically-created triggers https://developers.google.com/apps-script/reference/script/script-app Because your spreadsheets have lots of rows, you may hit the execution quota anyway - by using triggers you can work around the limitation. 如果您在运行脚本的时间方面需要更大的灵活性,请查看动态创建的触发器https://developers.google.com/apps-script/reference/script/script-app因为电子表格有很多行,所以您可能仍然会达到执行配额-通过使用触发器,您可以解决限制。

Finally, if a cell containing '----' is a rare occurrence, it might be better to create another array variable with new values and row numbers to update than updating the entire range. 最后,如果包含'----'的单元格很少出现,那么最好使用另一个值和行号来创建另一个数组变量以进行更新,而不是更新整个范围。 Personally, I think the single range update action would still be quicker, but you could try both approaches and see which one works best. 就个人而言,我认为单一范围更新操作仍然会更快,但是您可以尝试两种方法,然后看看哪种方法最有效。

function onOpen(){

 test();


}

function onEdit() {

 test();

}


function test() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('yourSheetName');

  //range to replace values in
  var range = sheet.getRange(2, 4, sheet.getLastRow() - 1, 1);

  //range to get new values from
  var lookupRange = range.offset(0, -2);

  //2d array of values from the target range
  var values = range.getValues();
  //2d array of values from the source range
  var lookupValues = lookupRange.getValues();   

  //looping through the values array and checking if array element meets our condition
  for (var i=0; i < values.length; i++) {

      values[i][0] = (values[i][0] == '------') ? lookupValues[i][0] : values[i][0];

  }

  // one method call to update the range
  range.setValues(values);

}

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

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