[英]Google Sheets Apps Script -- select value from cell with multiple lines
This is in GoogleSheets, using AppsScript.这是在 GoogleSheets 中,使用 AppsScript。
Suppose I have a cell in A1 that contains the following value:假设我在 A1 中有一个包含以下值的单元格:
Dog Dog
Cat Cat Cat
Lion
I know I can select the entire contents of the cell using .getValue()
, but how would I select just the fist line, Dog Dog
, or just the second line, Cat Cat Cat
?我知道我可以 select 使用
.getValue()
单元格的全部内容,但我怎么会 select 只是第一行, Dog Dog
,或者只是第二行, Cat Cat Cat
? Each line may have multiple words associated to it, so I can't simply split by using the space.每行可能有多个与之关联的单词,所以我不能简单地使用空格来分割。
Great question.好问题。 You can still use
.getValue()
but you will need to add one more piece to break apart the value to the desired string.您仍然可以使用
.getValue()
但您需要再添加一块以将值分解为所需的字符串。 value.split("\n")[0];
will get the first linebreak listed.将列出第一个换行符。 If you want to get a different line then change
[0]
to another number.如果您想换行,请将
[0]
更改为另一个数字。
function selectSingleLine() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var tab = ss.getSheetByName('Sheet1');
var value = tab.getRange(1,1).getValue();
var firstBreak = value.split("\n")[0];
var secondBreak = value.split("\n")[1];
}
You can split by using char(10)您可以使用 char(10) 进行拆分
=split(A1,char(10))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.