[英]Replace user in a protected range by script (Google Sheet)
這是我的具體情況:
這里是代碼,但在邏輯上有些錯誤,我懷疑什么是數組,什么不是..
var Protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for (var p = 0; p < Protections.length; p++) {
var Protection_Desc = Protections[p].getDescription();
var Protection_Editors = [];
var Protection_Editors = [Protections[p].getEditors()];
for (var r = 0; r < Protection_Editors.length; r++){
var Protection_Email[r] = [Protection_Editors[r].getEmail()];
if (Protection_Idontknow == Editor2Replace){
Protections[p].addEditor = Editor2Add;
Protections[p].removeEditor = Editor2Replace;
var Protection_Range = Protections[p].getRange();
var Protection_Row = Protection_Range.getRow();
var Owner1 = sheet.getRange(Protection_Row,5).getValue();
var Owner2 = sheet.getRange(Protection_Row,6).getValue();
if (Owner1 == Editor2Replace){
sheet.getRange(Protection_Row,5).setValue(Editor2Add);
}
if (Owner2 == Editor2Replace){
sheet.getRange(Protection_Row,6).setValue(Editor2Add);
}
}
}
非常感謝您的幫助
您的腳本中有很多問題,我將一一列舉。 此外,我可以通過修改您的腳本來替換受保護工作表中的用戶。
var Protection_Editors = [];
var Protection_Editors = [Protections[p].getEditors()];
var Protection_Editors = [Protections[p].getEditors()];
...
var Protection_Email[r] = [Protection_Editors[r].getEmail()];
var Protection_Email[r] = [Protection_Editors[r].getEmail()];
Protection_Idontknow
if (Protection_Idontknow == Editor2Replace){
addEditor
和removeEditor
的錯誤使用Protections[p].addEditor = Editor2Add;
Protections[p].removeEditor = Editor2Replace;
下面的代碼應該解決這些問題(添加了一些評論):
var Protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for (var p = 0; p < Protections.length; p++) {
var Protection_Desc = Protections[p].getDescription();
// returned value of getEditors is already an array, return as is
var Protection_Editors = Protections[p].getEditors();
for (var r = 0; r < Protection_Editors.length; r++) {
var Protection_Email = Protection_Editors[r].getEmail();
// compare current email with the one you want to replace
if (Protection_Email == Editor2Replace) {
// add new and remove the one to replace
Protections[p].addEditor(Editor2Add);
Protections[p].removeEditor(Editor2Replace);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.