如何为电子表格声明可变范围? [英] How to declare a variable range to a spreadsheet?

查看:96
本文介绍了如何为电子表格声明可变范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法设置变量以将数字返回到Google表格中K列下的最后一行.代码在我已经将exampleText定义为变量的最后一行.setValues(exampleText)上失败.

I can't set a variable to return the number to the last row under the K column in google sheets. The code fails on the last line .setValues(exampleText) where I have already defined exampleText as a variable.

我使用了多种设置变量的方法,包括为列设置变量.

I have used different methods of setting variables including setting a variable for a column.

我收到以下错误:

找不到方法getRange(number,number)."

"Cannot find method getRange(number,number)."

前三行是我之前尝试过的示例.

The top three rows are an example I tried before.

Column = 'K' 
range = ss.getRange(LastRow, Column)
range.setValue(exampleText)

var ss = SpreadsheetApp.getActiveSpreadsheet()

var column = 11

var lastRow = ss.getLastRow();

ss.getRange(lastRow, column)
.setValues(exampleText)

推荐答案

问题:

  • getRange是在Spreadsheet类和Sheet类上都可用的方法.

    Issue:

    • getRange is a method that's available both on Spreadsheet class and Sheet class.

      但是,只有 getRange(string) 在电子表格上可用.因此,您可以使用ss.getRange('Sheet1!A1:B4').

      getRange(number,number) 仅在图纸上可用.因此,您可以使用ss.getActiveSheet().getRange(1,4).

      Sheet类还接受此方法的其他变体,但Spreadsheet类不接受,该类仅接受string作为唯一参数.

      Sheet class also accepts other variations of this method, but Spreadsheet class doesn't, which only accepts string as the only parameter.

      有时,您还会收到

      异常:参数(字符串,数字,数字,数字)与SpreadsheetApp.Spreadsheet.getRange的方法签名不匹配

      Exception: The parameters (String,number,number,number) don't match the method signature for SpreadsheetApp.Spreadsheet.getRange

      第一个参数是字符串的原因是因为JavaScript引擎试图匹配实际的函数调用签名:getRange(string).它将第一个数字转换为字符串,但不知道如何处理其余参数(数字).由于没有与Spreadsheet类上的调用(字符串,数字,数字,数字)匹配的方法签名,因此会引发错误.

      The reason the first parameter is a string is because there is a attempt by JavaScript engine to match the actual function call signature: getRange(string). It converts the first number to string, but it doesn't know what to do with the rest of the parameters(numbers). As there is no method signature that match the call (string, number,number,number) on Spreadsheet class, it throws a error.

      如官方文档中所述,对预期的类使用正确的方法.对于大多数目的,您必须使用Sheet类.

      Use proper methods on the intended class as described in the official documentation. For most purposes, You must use the Sheet class.

      SpreadsheetApp.getActive().getSheets()[0].getRange(1,1)
      

      这篇关于如何为电子表格声明可变范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆