2

I recently started messing around with google script for spreadsheets and I was wondering if there is a way to change a cells value based on the currently selected cell? For example, if the cell displaying the row number is in A1 and the user highlights B8, A1 would display '8'. Is that even possible?

Flipping through the google apps-script spreadsheet services page and I noticed a class called .getActiveCell and I'm assuming that would be a key part but I'm kind of lost. Can anyone point me in the right direction?

Thank you for any help.

4

2 回答 2

1

这个小代码片段可以部分满足您的需要,但无法检测到简单的单元格选择,它仅在修改单元格内容时触发。

function onEdit(){
  var ss = SpreadsheetApp.getActive();
  var cell = ss.getActiveCell();
  ss.getRange('A1').setValue(cell.getA1Notation().replace(/[^0-9]/g, ''));// get A1 notation and rip off any non numeric value to keep only the row number
}

据我所知,没有直接的方法可以通过简单的点击或按键来获得选择。

于 2013-09-30T20:27:47.443 回答
0

Google 电子表格脚本允许注册onChange(e)触发器:

可安装触发器

function createSpreadsheetEditTrigger() {
  var ss = SpreadsheetApp.getActive();
  ScriptApp.newTrigger('myFunction')
      .forSpreadsheet(ss)
      .onChange()
      .create();
}

我还没有尝试过在更改选择时是否会拍摄。

于 2015-06-15T12:36:46.507 回答