2017-11-17 117 views
0

在用戶編輯單元格(例如C3)時處理表格,它會插入他/她的電子郵件(名稱)。有一個腳本只能用於一列(「C」),但當移動到另一個表或列(「5或E」)時,它會給出「錯誤 - 您沒有權限調用getActiveUser(第41行)」。
因爲它的工作一次,不知道我失蹤或者爲什麼同樣的腳本不會在所有的另一個頁面上的任何援助工作。謝謝。如何在編輯表格單元格後插入用戶電子郵件

function onEdit() { 
var s = SpreadsheetApp.getActiveSheet(); 
if(s.getName() == "Sheet1") { //checks that we're on the correct sheet 
    var r = s.getActiveCell(); 
    var email = Session.getActiveUser().getEmail(); 
    Logger.log(email); 
    if(r.getColumn() == 3) { //checks the C column 
    var nextCell = r.offset(0, 1); 
    nextCell.setValue(email); 
    if(r.getColumn() == 5) { //checks the E column 
    var nextCell = r.offset(0, 1); 
    if(nextCell.getValue() !== '') //is empty? 
nextCell.setValue(email); 
    } 
    } 
} 
+0

此代碼的格式有點混亂,嵌套的「if」語句可排隊以幫助w然後可以有答案。 – Ryanman

回答

0

確保您的電子表格的所有者和你已授予必要的權限以運行該腳本

function onEdit() { 
    var s = SpreadsheetApp.getActiveSheet(); 
    if (s.getName() == "Sheet2") { //checks that we're on the correct sheet 
     var r = s.getActiveCell(); 
     var email = Session.getActiveUser().getEmail(); 
     if (r.getColumn() == 3) { //checks the C column 
      var nextCell = r.offset(0, 1); 
      nextCell.setValue(email); 
     } 
     if (r.getColumn() == 5) { //checks the E column 
      var nextCell = r.offset(0, 1); 
      Logger.log(nextCell.getValue()); 
      if (nextCell.getValue() == '') //is empty? 
       nextCell.setValue(email); 
     } 
    } 
} 
相關問題