google 计算单元格中字符数的应用程序脚本

google apps script counting characters in a cell

我正在尝试找到一种方法来调整合并的单元格组以显示合并时包含在第一个单元格中的所有文本字符。我认为会有一种简单的方法来计算第一个单元格中的字符数,然后我可以通过开发一个公式来调整一个或多个单元格的高度(例如每 30 个字符添加 .2)。

我正在使用以下代码尝试计算字符数:

var tempValue;
var tempCount = 0;

    tempValue = sprintSheet.getRange("D3").getDisplayValue();
    tempCount = tempValue.length();

不幸的是,我在最后一行收到以下错误:

TypeError: Cannot call property length in object

我似乎无法从范围/值过渡到文本以使用长度 属性。

字符串长度可作为 属性 而不是函数调用。

https://www.w3schools.com/jsref/jsref_length_string.asp

tempCount = tempValue.length;

简答

Use tempCount = tempValue.length 而不是 tempCount = tempValue.length();

说明

Google Apps 脚本基于 JavaScript。 getDisplayValue() returns 一个 JavaScript 字符串原语。原始数据类型可以使用长度 属性,并使用 .length 调用它(注意不使用括号)。

参考资料