(javascript)如何从一个单元格指向列中的所有单元格



我有一个简单的代码,当有人从下拉菜单中选择名称时(数据验证(,它会要求输入密码。我的问题是它只指向一个单元格,我应该添加什么或做什么来使它指向列中的所有单元格?请参阅下文。

// define each user password
const userPasswords = { 
"Guilherme Cudessuata": "123",
"Manuel Jose": "456",
"Emaculada": "789",
"Jonata Kedia": "111"
}
function onEdit(e) {
const namesCell = "E4" // the cell with the dropdown

if (e.range.getA1Notation() != namesCell) {
return
}
// show prompt for password:
var ui = SpreadsheetApp.getUi()
const response = ui.prompt("Password protected range", 
"Please enter your password:",
ui.ButtonSet.OK_CANCEL)
const button = response.getSelectedButton()
const text = response.getResponseText() 
// don't run if the user pressed cancel or close
if (button != ui.Button.OK) {
return
}
// check the passwords
if (text != userPasswords[e.value]) {
ui.alert('Incorrect Password', 'Please try again', ui.ButtonSet.OK);
e.range.setValue(e.oldValue) // reset to old value
}
}

试试这样的东西:

function onEdit(e) {
const sheetName = "Sheet1" //change to suit
const namesColumn = 5 // the column with the dropdown
const startRow = 4 //change to suit
const endRow = 100 //change to suit
if (e.range.getSheet().getName() != sheetName || e.range.columnStart != namesColumn || e.range.rowStart < startRow || 
e.range.rowEnd > endRow) {
return;
}
//rest of your code
}

看看这是否有帮助?

参考:

  • 事件对象:编辑

相关内容

最新更新