2017-03-25 24 views
0

有誰知道是否有訪問舊的(即預編輯)公式的一種方式,而不是屬性oldValue,編輯事件對象的?我以爲e.oldFormula會工作,但它是未定義的,甚至沒有在文檔中。如何在谷歌工作表老去式(不屬性oldValue)從onEdit()腳本

我工作的一個腳本來要求輸入密碼進行編輯時,一定範圍內,但是,爲了撤消編輯,如果用戶未能提供正確的密碼,我需要知道什麼是小區前。對於包含數字或字符串細胞,e.oldValue的偉大工程,但最讓我想保護細胞的包含公式(這就是爲什麼我要保護它們),e.oldValue從像它應該停止更新數據。

這裏有我有,如果電池不包含公式(比其他只適用,它的偉大工程的代碼:

// Prompt for password before allowing edit of formula cells 
function onEdit(e) { 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var sheet = ss.getActiveSheet(); 
    var cell = sheet.getActiveCell(); 
    var editRange = e.range; 
    var editCol = editRange.getColumn(); 


    if (editCol != 4) { 
    var password = "321"; // not actual password 
    var passwordAttempt = Browser.inputBox('Enter Password to edit protected range, or hit cancel to cancel edit:', Browser.Buttons.OK_CANCEL); 

    if(passwordAttempt == password) { 
     Browser.msgBox('Edit Accepted'); 
    } else { 
     if (passwordAttempt != 'cancel'){ 
     Browser.msgBox('Incorrect password. Contact James Atkins for password to edit protected ranges.'); 
     } 
     if(e.oldValue){ 
     Browser.msgBox('old value is defined as ' + e.oldFormula); 
     e.range.setValue(e.oldValue); 
     } else { 
     e.range.clear(); 
    } 
    } 

通知你,我這樣做是因爲內置的保護範圍在Google表格中,不會區分用戶是通過手動修改單元格還是通過激活改變內容的腳本來更改單元格的內容,我希望允許所有用戶激活排序和清除特定範圍的腳本,但不允許它們亂用手動的內容。

+0

如果它不存在的官方文檔中,那麼它不存在。向Google團隊建議 –

+0

如果用戶可以激活腳本並編輯單元格,他們也可以看到腳本和密碼。不是嗎? –

+0

我不確定這是否有幫助,但高級雲端硬盤服務可讓您訪問修訂版。也許你可以通過這種方式獲得舊的價值。 [先進的驅動服務(https://developers.google.com/apps-script/advanced/drive) – Cooper

回答

0

你可以通過創建表的副本,讓您的FORMUL跟蹤解決這個問題一個。使克隆表關閉最終用戶的限制。所以他們不能編輯它。

function onEdit(e) { 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var sheet = ss.getActiveSheet(); 
    var cloneSheet = ss.getSheetByName("cloneSheet") 
    var cell = sheet.getActiveCell(); 
    var editRange = e.range; 
    var cloneRange = cloneSheet.getRange(editRange.getA1Notation()) 
    var editCol = editRange.getColumn(); 


    if (editCol != 4 && sheet.getName() != "cloneSheet") { 
    var password = "321"; // not actual password 
    var passwordAttempt = Browser.inputBox('Enter Password to edit protected range, or hit cancel to cancel edit:', Browser.Buttons.OK_CANCEL); 

    if(passwordAttempt == password) { 
     Browser.msgBox('Edit Accepted'); 
     cloneRange.setValue(editRange.getFormula()) //modify the clone to match the new formula. 
    } else { 
     if (passwordAttempt != 'cancel'){ 
     Browser.msgBox('Incorrect password. Contact James Atkins for password to edit protected ranges.'); 
     } 
     if(e.oldValue){ 
     Browser.msgBox('old value is defined as ' + cloneRange.getFormula()); 
     e.range.setValue(cloneRange.getFormula()); 
     } else { 
     e.range.clear(); 
    } 
    } 
    } 
} 

但是,最終用戶仍然可以看到密碼。如果你真的是最終用戶只做有限的改變,下面的解決方案會更好。

替代的解決方案

當最終用戶執行一個腳本,即使你寫的腳本,它與最終用戶的範圍/身份運行。所以,如果他們沒有編輯權限,那麼運行在他們別名下的腳本也沒有它。這是對您的評論迴應:

谷歌工作表不區分用戶通過手動編輯更改單元格的內容和激活更改內容的腳本。

因此,如何解決這個問題,您可以使用谷歌網絡應用程序來運行排序和清除腳本。最終用戶使用一個腳本,使用UrlFetch調用這個腳本,這解決了範圍問題,並防止他們看看你的代碼/密碼等。

這是SO問題,解決類似的問題: Google Apps Script: Temporarily run script as another user

相關問題