简体   繁体   English

Google Sheets Apps 脚本——来自多行单元格的 select 值

[英]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.

相关问题 如何通过 Apps Script 更改 Google Sheets 中的多个单元格值? - How to change multiple cell values in Google Sheets via Apps Script? Google Sheets Apps 脚本,不会注册单元格值 - Google Sheets Apps Script, won't register cell value Google Script 和 Google Sheets - 下拉选择单元格并从侧边栏输入添加值 - Google Script and Google Sheets - Drop down to select cell and add value from input from sidebar 无法从Google Apps脚本的多个工作表中获取范围 - Cannot get ranges from multiple sheets in google apps script 通过 Google Apps 脚本在 Google 表格中匹配整个单元格以进行多次查找和替换 - Match Entire Cell for Multiple Find and Replace in Google Sheets via Google Apps Script 如何在谷歌应用程序脚本的 forEach 循环中设置谷歌表格中目标单元格的值? - How can you set the value of a target cell in google sheets within a forEach loop in google apps script? 使用选择多个使用谷歌应用程序脚本将多个值发送到谷歌表格 - Using select multiple to send multiple values to google sheets using google apps script 编辑谷歌表格应用脚本中过滤行中的单元格值 - Edit cell value from filtered row in app script for google sheets 如何将 Google Apps 脚本应用于多个工作表 - How to apply Google Apps Script to multiple sheets Google脚本根据单元格值或日期范围添加多行 - Google Script adding multiple lines based on cell value or date ranges
 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM