如何在谷歌表格中更改“Change ByVal Target As Range”

问题描述 投票:0回答:1

在谷歌表格中选择下拉菜单后如何更改单元格?

我有 VB 代码,但无法将其转换为 App 脚本

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 Then
Target.Offset(0, 1).Select
ElseIf Target.Column = 2 Then
Target.Offset(1, -1).Select
End If
End Sub

你能帮我解决这个问题吗,非常感谢!

google-sheets google-apps-script
1个回答
0
投票

使用onEdit(e)

如果您正在编辑 A 列中的内容,您当前使用的 VB 脚本将仅选择 B 列上的单元格,反之亦然。在 Google Apps 脚本中,您的 VB 脚本应该如下所示:

function onEdit(e) { //triggers the script when you edit any cell on the spreadsheet
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); //gets the active sheet you are currently editing
  var col = e.range.getColumn(); //gets the column number of the cell you are currently editing
  var row = e.range.getRow(); //gets the column of the cell you are currently editing

  if (col == 1) {
    ss.getRange(row, col + 1).activate(); //selects the cell on column 2
  }
  else if (col == 2) {
    ss.getRange(row, col - 1).activate(); //selects the cell on column 1
  }
}

您只需保存此脚本,当您对 A 列或 B 列进行编辑时,它会自动运行。结果应该是这样的:

Output

参考:

© www.soinside.com 2019 - 2024. All rights reserved.