如何允许Google工作表中的共享用户运行访问受保护工作表的脚本

时间:2014-01-10 22:03:34

标签: google-apps-script google-sheets

我有一个谷歌电子表格,我打算与超过50个用户共享,每个用户都有自己的工作表。对于安全措施,我有一些我想要运行的脚本,允许用户在其工作表中输入数据但阻止他们删除该条目。代码在我的最终工作正常,但是当我尝试通过将其分享给其中一个用户来测试它时,脚本要么没有运行,要么不允许运行。

我已经对此问题进行了一段时间的研究,似乎无法应用我在本论坛和其他许多论坛上发布的任何解决方案。我正在使用onEdit()函数,据我所知,这是一个简单的触发器,所以它不应该导致这种错误。代码如下:

function onEdit(event) {

var masterSheetName = "Blank" // sheet where the cells are protected from updates
  var helperSheetName = "Blank Copy" // sheet where the values are copied for later checking

  // range where edits are "write once": D18:Y157, i.e., rows 18-157 and columns 4-25
  var firstDataRow = 18; // only take into account edits on or below this row
  var lastDataRow = 157; // only take into account edits on or above this row
  var firstDataColumn = 4; // only take into account edits on or to the right of this column
  var lastDataColumn = 25; // only take into account edits on or to the left of this column

  var miscFirstDataColumnOne = 15; // only take into account edits on or to the right of this column
  var miscLastDataColumnOne = 15; // only take into account edits on or to the left of this column
  var miscFirstDataColumnTwo = 25; // only take into account edits on or to the right of this column
  var miscLastDataColumnTwo = 25; // only take into account edits on or to the right of this column
  var miscFirstDataRowTwo = 18;
  var miscLastDataRowTwo = 157;

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var masterSheet = ss.getActiveSheet();
  var masterSheetMiscOne = ss.getActiveSheet();
  if (masterSheet.getName() != masterSheetName) return;
  if (masterSheetMiscOne.getName() != masterSheetName) return;

  var masterCell = masterSheet.getActiveCell();
  var masterCellMiscOne = masterSheetMiscOne.getActiveCell();

  if (masterCell.getRow() < firstDataRow || masterCell.getColumn() < firstDataColumn ||

    masterCell.getRow() > lastDataRow || masterCell.getColumn() > lastDataColumn) return;

  var helperSheet = ss.getSheetByName(helperSheetName);
  var helperCell = helperSheet.getRange(masterCell.getA1Notation());
  var newValue = masterCell.getValue();
  var oldValue = helperCell.getValue();

  var user = SpreadsheetApp.getActive().getEditors()[1];
  var permission = helperSheet.getSheetProtection();
  permission.addUser(user);
  helperSheet.setSheetProtection(permission);
  SpreadsheetApp.flush();

  if (oldValue == "") {
    helperCell.setValue(newValue);
  } else {
    masterCell.setValue(oldValue);
    Browser.msgBox('You can not delete this value');
  }

  if ((masterCellMiscOne.getRow() < firstDataRow || masterCellMiscOne.getColumn() < miscFirstDataColumnOne || masterCellMiscOne.getRow() > lastDataRow || 

    masterCellMiscOne.getColumn() > miscLastDataColumnOne) & (masterCellMiscOne.getRow() < firstDataRow || masterCellMiscOne.getColumn() < miscLastDataColumnOne ||

      masterCellMiscOne.getRow() > lastDataRow || masterCellMiscOne.getColumn() > miscLastDataColumnTwo)) return;

var miscCellValueOne = masterCellMiscOne.getValue();

  if (miscCellValueOne !== "")  {

    Browser.msgBox('Submission Needs To Be Authorized Before Being Added.');

  }

  SpreadsheetApp.flush();
  permission.removeUser(user)
  helperSheet.setSheetProtection(permission)
}
}

每个用户都有一张也有副本的工作表(在此代码中,用户工作表为“空白”,副本为“空白复制”)。空白副本将受到保护,因此无法对其进行编辑,因为这样可以删除其工作表中的数据(空白)。这段代码确实有效,但我只需要在共享电子表格时使用它。

非常感谢所有帮助,此处是指向电子表格副本的链接。

https://docs.google.com/spreadsheet/ccc?key=0AhBLjhwt88kUdFZ2cG9CVFNEQy1zVHdJYlp6ZEx5Unc&usp=sharing

1 个答案:

答案 0 :(得分:1)

简短答案:创建具有最少代码重构功能的简单加载项

说明

  

...但是当我尝试通过与一个用户共享进行测试时,脚本要么未运行,要么不允许运行。

不幸的是,您当前正在使用“触发器”(即onEdit)-

  

...无法访问需要servicesauthorization

请参阅Simple Triggers > Restrictions

替代解决方案

如果仅来自您所在域的人员将使用工作表/脚本,则可以引入onInstall函数并将此代码作为可见性设置为Private的附件发布。

希望这会有所帮助。