2017-02-23 51 views
1

我有一個文檔加載項,我需要通過調用onOpen(e)函數中的警報來顯示用戶有關其新更新和功能的信息。一個重要的點,一生中只有一次僅運行一次DocumentApp.getUi()警報以通知新功能

OR

就像一個不再次顯示按鈕。

我該怎麼做?

function onOpen(e) { 
    DocumentApp.getUi().createAddonMenu() 
     .addItem('ez-notes', 'Sidebar') 
     .addToUi(); 
    DocumentApp.getUi().alert("new feature..."); //will trouble user everytime. 
} 

回答

2

有一個叫onInstall()

保留函數名稱它用於加載項。它只在安裝附加組件時運行。

function onInstall() {} 

只要發佈您的加載項的新版本,就不會運行。如果您希望某些代碼僅在每個新發布的版本中運行一次,那麼您將需要存儲當前版本和用戶在某處使用的最新版本,然後在運行某些代碼時對其進行比較。您可以在「屬性服務」中的「腳本屬性」中保存版本號應該保存的內容,或者硬編碼當前版本號。每次運行oOpen()函數時,都需要運行一些服務器代碼,將當前版本與用戶使用的最後一個已知的已保存版本進行比較。

我,什麼也不做,只是返回了當前最新的版本應該是什麼樣的功能,我改變每當有新版本發佈,這個數字:

function newestVersion() {return "12";}// Return the newest version number 

function onOpen() { 
    var newestVersion,lastUsedVersion; 

    newestVersion = newestVersion();//Call function to get the newest version 
    lastUsedVersion = fncGetLastUsedVersion();//Run function to get last used version 

    if (lastUsedVersion !== newestVersion) { 
    //Display message 

    //Save new value of Last Used Version to User or Document Properties 

    } 
} 

function lastUsedVersion() { 
    //Get last used version from User or Document Properties 

} 
1

這裏是一個辦法做到這一點,這可能比桑迪的版本 - 更加複雜,但是我用它在過去的腳本:

var ss = SpreadsheetApp.getActiveSpreadsheet(); 
 

 
function onOpen() { 
 
    
 
    var documentProperties = PropertiesService.getDocumentProperties(); 
 
    Utilities.sleep(100) 
 
    var checkOpen = documentProperties.getProperty('checkOpenStatus'); 
 
    Utilities.sleep(100) 
 

 
    
 
    if(checkOpen == "true") { 
 

 
    } 
 
    else { 
 
    newfeature(); 
 
    } 
 
    }; 
 

 
    
 
    function newfeature() { 
 
    var doc = SpreadsheetApp.getActiveSpreadsheet(); 
 
    var app = UiApp.createApplication().setTitle('New feature'); 
 
    var panel = app.createVerticalPanel(); 
 
     
 
    // add the html panel 
 
    app.add(app.createHTML("<b>New features</b><br><br><b>1.</b>New feature text")); 
 
    
 
     // adds checkbox 
 
    var cBox = app.createCheckBox("Do Not show this on Load").setName("chk1").setId("chk1"); 
 
    
 
    // set check box stuff 
 
    var cBoxClick = app.createServerClickHandler('checked'); 
 
    cBoxClick.addCallbackElement(cBox); 
 
    cBox.addClickHandler(cBoxClick); 
 
    app.add(cBox); 
 
    app.add(panel); 
 
    doc.show(app); 
 
    return app; 
 
    
 
    } 
 

 
    function checked(e) { 
 
    var app = UiApp.getActiveApplication(); 
 
    
 
    if (e.parameter.chk1 == "true") 
 
    { 
 
    var documentProperties = PropertiesService.getDocumentProperties(); 
 
    Utilities.sleep(100) 
 
    documentProperties.setProperty('checkOpenStatus', 'true'); 
 
    } 
 
    else 
 
    { 
 
    var documentProperties = PropertiesService.getDocumentProperties(); 
 
    Utilities.sleep(100) 
 
    documentProperties.setProperty('checkOpenStatus', 'false'); 
 
    } 
 
    return app; 
 
    }