0

截至目前,谷歌电子表格似乎不允许锁定单元格范围,而是必须在整个工作表的基础上进行锁定。我想与另一个用户共享一个工作表,并让他们在工作表 1 上输入数据。为了防止篡改,我希望在工作表 2 上复制的数据(由我锁定)以及工作表 1 上最后一次更改的时间戳。我一直在玩这个 onEdit() 函数,但是当我编辑工作表 1 时它没有给出更新的时间,但仍然只有在我编辑工作表 2 时才给出。我只是还没弄清楚我做错了什么。

function onEdit(e) 
{
  var ss = e.source.getActiveSheet();
  var rr  = e.source.getActiveRange();

//comment 2 lines below if you want it working on all sheets, not just on 2nd one
  if(ss.getIndex()!= 2)
    return;
 ///
  var firstRow = rr.getSheetByName(Sheet2).getRow();
  var lastRow = rr.getSheetByName(Sheet2).getLastRow();

//the last modified date will appear in 12th column
  for(var r=firstRow; r<=lastRow; r++)
    ss.getRange(r, 12).setValue(new Date());
}

还有其他方法可以做到这一点吗?

4

2 回答 2

0

您不能使用脚本来防止其他用户篡改,因为他可以转到工具>脚本编辑器并编辑您的脚本,使其空闲,然后编辑 Sheet1,然后恢复您的脚本。请参阅此问题的问题跟踪器。您无法保护您的脚本不被与您共享至少一个电子表格工作表的其他用户编辑。

另一方面,抛开脚本,您始终可以查看由谁以及何时编辑的历史:文件>查看修订历史。

于 2012-08-02T23:05:19.917 回答
0

I see a couple of problems. First, you're trying to use .getSheetByName() on a range object, but that method is only supported by spreadsheet objects. You can run .getSheet() on a range and that will return the sheet the range is in. Secondly, formatting: GAS requires standard Javascript which means that your if and for functions need to have {}. This should work for what you want:

onEdit(e){
if(e.range.getSheet().getSheetName()!="Sheet2"){
 var row1=e.range.getRow();
 var col2=e.range.getColumn();
 var row2=e.range.getLastRow();
 var col2=e.range.getLastColumn();
 var data=e.range.getValues();//the data to be copied
 var copySheet=e.source.getSheetByName("Sheet2");//sheet you want to copy to
 var copyRange=copySheet.getRange(row1,col1,row2-row1,col2-col1);// the range data will be copied to
 var time=new Date();
 var timeRange=copySheet.getRange(row,col);//wherever you want the time signature to be. 
 timeRange.setValue(time);//inserts time stamp into sheet
 copyRange.setValues(data);//copies data to the range
}
}

Just replace row and col with the row and column you want to place the time into. It takes the edited range and copies it to the same location on sheet 2 so it only works if sheet 1 is the only sheet to be edited. Otherwise you'll end up with overlapping data. You could also set a time stamp to be adjusted to the range, meaning put its position as something like (row1,col2+5). That would put it 5 columns right of the edited range.

于 2015-08-22T17:23:29.957 回答