如何根据下拉菜单选择更改范围
How to change the range based on dropdown menu selection
我有一个 Google Sheet,我在其中跟踪我的高中棒球队的棒球统计数据。每次记录命中错误或 运行 时,我都有脚本设置,我有一个 onEdit(e) 复选框,用于将 +1 添加到总统计数据中。
//Away Run
function increment1() {
SpreadsheetApp.getActiveSheet().getRange('X3').setValue(SpreadsheetApp.getActiveSheet().getRange('X3').getValue() + 1);
}
function onEdit(e) {
if (e.range.getA1Notation() == 'G10')
SpreadsheetApp.getActiveSheet().getRange('X3').setValue(SpreadsheetApp.getActiveSheet().getRange('X3').getValue() + 1);
}
我有一个记分牌部分和一个列出所有局数的下拉菜单;我想将复选框与总运行的特定范围相关联,并且 也与与当前所选下拉菜单相关联的每个范围相关联。
Image of the Google Sheet Scoreboard
为了清楚起见,将其进一步分解。当前,当编辑“G10”(选中或取消选中复选框)时,“X3”对当前值有 +1,另外我希望根据“I2”下拉菜单中选择的内容将 +1 添加到不同的范围”。因此,如果在下拉列表中选择“Top 1”,则在编辑“G10”时,“P3”的值将增加 1,依此类推,基于所选的 Inning
如果选择“Bot 1”,则在编辑“G10”+1 到“P4”“Top 2”+1 到“Q3”、“Top 3”+1“R3”、“Bot 3”时+1“R4”一直到“Bot 7”+1“V4”
Link to a live copy version of the Sheet
[What Happens] [3]
[What I want on one click][4]
[Change of dropdown menu what happens][5]
[Change of Dropdown what I want to happen][6]
[What I want to happen][7]
根据您的以下回复,
I have simplified everything all I want to do is click the check box and have a dropdown menu pointing to a range to add 1 digit to the range selected - Pics in order
在这种情况下,下面的示例脚本怎么样?
示例脚本:
很遗憾,我无法理解复选框和下拉列表的单元格坐标。因此,在此示例中,复选框和下拉列表分别放在单元格“A1”和“B1”中。
function onEdit(e) {
const obj = {
checkbox: "A1", // Please set the cell coordinate of checkbox.
dropdownlist: "B1", // Please set the cell coordinate of dropdown list.
values: { "K2": "K2", "K3": "K3", "L2": "L2", "L3": "L3" } // Please set the values and the cell coordinates in the dropdownlist.
};
const sheetName = "Sheet2";
const range = e.range;
const sheet = range.getSheet();
if (sheet.getSheetName() != sheetName || range.getA1Notation() != obj.checkbox) return;
const checkboxValue = sheet.getRange(obj.dropdownlist).getValue();
if (!obj.values[checkboxValue]) return;
const editCell = sheet.getRange(obj.values[checkboxValue]);
const value = editCell.getValue();
editCell.setValue(value ? value + 1 : 1);
}
- 例如,如果要在“K2”的值下的“K5”单元格中添加
1
,请将values: { "K2": "K2", "K3": "K3", "L2": "L2", "L3": "L3" }
修改为values: { "K2": "K5", "K3": "K3", "L2": "L2", "L3": "L3" }
。
测试:
当此脚本为运行时,得到如下结果
我有一个 Google Sheet,我在其中跟踪我的高中棒球队的棒球统计数据。每次记录命中错误或 运行 时,我都有脚本设置,我有一个 onEdit(e) 复选框,用于将 +1 添加到总统计数据中。
//Away Run
function increment1() {
SpreadsheetApp.getActiveSheet().getRange('X3').setValue(SpreadsheetApp.getActiveSheet().getRange('X3').getValue() + 1);
}
function onEdit(e) {
if (e.range.getA1Notation() == 'G10')
SpreadsheetApp.getActiveSheet().getRange('X3').setValue(SpreadsheetApp.getActiveSheet().getRange('X3').getValue() + 1);
}
我有一个记分牌部分和一个列出所有局数的下拉菜单;我想将复选框与总运行的特定范围相关联,并且 也与与当前所选下拉菜单相关联的每个范围相关联。
Image of the Google Sheet Scoreboard
为了清楚起见,将其进一步分解。当前,当编辑“G10”(选中或取消选中复选框)时,“X3”对当前值有 +1,另外我希望根据“I2”下拉菜单中选择的内容将 +1 添加到不同的范围”。因此,如果在下拉列表中选择“Top 1”,则在编辑“G10”时,“P3”的值将增加 1,依此类推,基于所选的 Inning
如果选择“Bot 1”,则在编辑“G10”+1 到“P4”“Top 2”+1 到“Q3”、“Top 3”+1“R3”、“Bot 3”时+1“R4”一直到“Bot 7”+1“V4”
Link to a live copy version of the Sheet
[What Happens] [3]
[What I want on one click][4]
[Change of dropdown menu what happens][5]
[Change of Dropdown what I want to happen][6]
[What I want to happen][7]
根据您的以下回复,
I have simplified everything all I want to do is click the check box and have a dropdown menu pointing to a range to add 1 digit to the range selected - Pics in order
在这种情况下,下面的示例脚本怎么样?
示例脚本:
很遗憾,我无法理解复选框和下拉列表的单元格坐标。因此,在此示例中,复选框和下拉列表分别放在单元格“A1”和“B1”中。
function onEdit(e) {
const obj = {
checkbox: "A1", // Please set the cell coordinate of checkbox.
dropdownlist: "B1", // Please set the cell coordinate of dropdown list.
values: { "K2": "K2", "K3": "K3", "L2": "L2", "L3": "L3" } // Please set the values and the cell coordinates in the dropdownlist.
};
const sheetName = "Sheet2";
const range = e.range;
const sheet = range.getSheet();
if (sheet.getSheetName() != sheetName || range.getA1Notation() != obj.checkbox) return;
const checkboxValue = sheet.getRange(obj.dropdownlist).getValue();
if (!obj.values[checkboxValue]) return;
const editCell = sheet.getRange(obj.values[checkboxValue]);
const value = editCell.getValue();
editCell.setValue(value ? value + 1 : 1);
}
- 例如,如果要在“K2”的值下的“K5”单元格中添加
1
,请将values: { "K2": "K2", "K3": "K3", "L2": "L2", "L3": "L3" }
修改为values: { "K2": "K5", "K3": "K3", "L2": "L2", "L3": "L3" }
。
测试:
当此脚本为运行时,得到如下结果