获取更改特定单元格

时间:2016-10-29 17:16:06

标签: google-apps-script google-sheets

我正在尝试使用可以由受邀任何人编辑的表制作电子表格。但是,用户只能写入空的单元格或由他填充的单元格。他无法覆盖别人的工作。

我正在考虑将编辑器电子邮件及其单元格符号保存到属性中,但使用Session.getActiveUser().getEmail()SpreadsheetApp.getActive().getActiveRange().getA1Notation()和触发器onEdit我无法区分两个人,如果添加了某些内容与此同时,我无法分辨谁在做什么......至少我认为这是如何运作的。

由于

1 个答案:

答案 0 :(得分:0)

如果您拥有正常的Gmail帐户,则无法在onEdit触发器中访问Session.getActiveUser()<来源:https://developers.google.com/apps-script/reference/base/session#getActiveUser()

但我找到了一个很酷的解决方法:) 三重奏在于你无法将自己和所有者移除为编辑。因此,如果从受保护范围中删除所有编辑器,则将自己指定为编辑者(和所有者)。

在此脚本中,电子表格的所有者可以否决所有人。其他人将按照您的意愿行事:他们只能编辑自己的条目和空字段。

// 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({});
}