简体   繁体   English

有没有办法在多项选择中跨单元格迭代公式 - Google 工作表

[英]Is there a way to iterate a formula across cells in a multiple selection - Google sheet

In a google sheet, this simple script works to select a single cell, then click a button to decrease the value in the selected cell by 1:在谷歌表中,这个简单的脚本适用于 select 单个单元格,然后单击按钮将所选单元格中的值减 1:

function tally() {
 
  var cell = SpreadsheetApp.getActiveSheet().getActiveCell();
  var value = cell.getValue();
  cell.setValue(value-1);
}

Is there a way to do a multiple selection - ie, select multiple / scattered cells in a table, then iterate the function in each of the selected cells?有没有办法进行多项选择 - 即 select 表格中的多个/分散的单元格,然后在每个选定的单元格中迭代 function ?

Explanation:解释:

  • You can use getActiveRange and assuming that you have a 2D array, you can use double map to deduct one from each cell.您可以使用getActiveRange并假设您有一个二维数组,您可以使用双map从每个单元格中减去一个。
  • Then set all the values back to the sheet.然后所有值设置回工作表。

Solution:解决方案:

function tally() {
  const rng = SpreadsheetApp.getActiveSheet().getActiveRange();
  const values = rng.getValues().map(r=>r.map(c=>c-1));
  rng.setValues(values);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法在多个Google表格文件中编辑同一单元格? - Is there a way to edit the same cell across multiple Google Sheet Files? 用于对多个 Google 表格中的单个单元格求和的 JS 公式故障排除 - Troubleshooting JS formula for summing a single cell across multiple Google Sheets 满足多个列中的多个条件时发送电子邮件-Google表格脚本 - Send email when multiple conditions across multiple columns are met - Google Sheet Script 限制多个表上单元格的选择 - Limit selection of cells on multiple tables html表格中的多个单元格选择 - Multiple Cells Selection in Table in html 将公式从 JavaScript 添加到 CSV 以导入到 Google 表格会导致多列 - Adding Formula from JavaScript to CSV to be imported to Google Sheet results in multiple columns 有没有办法使用 html 和 js 按钮从特定的谷歌表格单元格中检索数据? - Is there a way to retrieve data from specific google sheet cells using html and js buttons? 如何在 App 脚本中循环 Google 表格公式 - How To Loop Google Sheet Formula In App Script 正则表达式在谷歌表格单元格中查找特定公式 - REGEX to find a specific formula in a google sheet cell 将表单上多个单元格的值复制提交到其他Google表格中的单个单元格中 - Copy values of multiple cells on form submit into a single cell in a different Google Sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM