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

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

问题描述

我无法设置变量以将数字返回到 Google 表格中 K 列下的最后一行.代码在最后一行 .setValues(exampleText) 失败,我已经将 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) 上可用.因此,您可以使用 ss.getRange('Sheet1!A1:B4').

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

      Sheet 类也接受此方法的其他变体,但 Spreadsheet 类不接受,它只接受字符串作为唯一参数.

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

      有时,您也会收到

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

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

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

      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天全站免登陆