Session.getActiveUser()是不是在onEdit觸發入店,如果你有一個正常的Gmail帳戶(來源:https://developers.google.com/apps-script/reference/base/session#getActiveUser())
但是我發現了一個非常酷的解決辦法:) 的TRIC謊言因爲你無法將自己和所有者作爲編輯者移除。因此,如果您從受保護的範圍中刪除所有編輯器,則您將自己指定爲編輯器(和所有者)。
在此腳本中,電子表格的所有者可以否決每個人。其他人將按照您的意願行事:他們只能編輯自己的條目和空白字段。
// Test it with colors
// var edittedBackgroundColor = "RED"; // makes the change visible, for test purposes
// var availableBackgroundColor = "LIGHTGREEN"; // makes the change visible, for test purposes
function onEdit(e) {
Logger.log(JSON.stringify(e));
var alphabet = "abcdefghijklmnopqrstuvwxyz".toUpperCase().split("");
var columnStart = e.range.columnStart;
var rowStart = e.range.rowStart;
var columnEnd = e.range.columnEnd;
var rowEnd = e.range.rowEnd;
var startA1Notation = alphabet[columnStart-1] + rowStart;
var endA1Notation = alphabet[columnEnd-1] + rowEnd;
var range = SpreadsheetApp.getActive().getRange(startA1Notation + ":" + endA1Notation);
if(range.getValue() === "") {
Logger.log("Cases in which the entry is empty.");
if(typeof availableBackgroundColor !== 'undefined' && availableBackgroundColor)
range.setBackground(availableBackgroundColor)
removeEmptyProtections();
return;
}
// Session.getActiveUser() is not accesible in the onEdit trigger
// The user's email address is not available in any context that allows a script to run without that user's authorization, like a simple onOpen(e) or onEdit(e) trigger
// Source: https://developers.google.com/apps-script/reference/base/session#getActiveUser()
var protection = range.protect().setDescription('Cell ' + startA1Notation + ' is protected');
if(typeof edittedBackgroundColor !== 'undefined' && edittedBackgroundColor)
range.setBackground(edittedBackgroundColor);
// Though neither the owner of the spreadsheet nor the current user can be removed
// The next line results in only the owner and current user being able to edit
protection.removeEditors(protection.getEditors());
Logger.log("These people can edit now: " + protection.getEditors());
// Doublecheck for empty protections (if for any reason this was missed before)
removeEmptyProtections();
}
function removeEmptyProtections() {
var ss = SpreadsheetApp.getActive();
var protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for (var i = 0; i < protections.length; i++) {
var protection = protections[i];
if(! protection.getRange().getValue()) {
Logger.log("Removes protection from empty field " + protection.getRange().getA1Notation());
protection.remove();
}
}
return;
}
function isEmptyObject(obj) {
for(var prop in obj) {
if(obj.hasOwnProperty(prop))
return false;
}
return JSON.stringify(obj) === JSON.stringify({});
}
在http://stackoverflow.com/questions/40322476/get-the-user-who-clicked-on-the-button-in-the-spreadsheet相同的註釋,\t 如果您正在使用一個「正常」的Gmail賬戶,當他訪問電子表格時,不可能獲得用戶的電子郵件。在商業或教育版本,你可以 –
@Sergeinsas是的。 – Wlad
不!你可以做到這一點。我發現這個非常酷的解決方法,請參閱我的評論! :) –