【发布时间】:2014-01-10 22:03:33
【问题描述】:
我有一个 google 电子表格,我计划与 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
【问题讨论】:
标签: google-apps-script google-sheets