2013-10-17 78 views
2

我是新手Google Apps腳本(和StackOverFlow太:D),並有一個問題,並不知道爲什麼:/ 這個問題似乎在我的ServerClickHandler ..我沒能解決與谷歌的問題:/ 當運行我的應用程序,並提交按鈕的錯誤消息被「時出現錯誤:要求:要求」 有誰知道爲什麼出現這種情況?「遇到錯誤:必需:」

var User = new Object(), 
Url = new Object(); 
// Startet die Web-App 
function doGet() { 
User.email = Session.getActiveUser().getEmail(); 

var app = UiApp.createApplication(); 
var panel = app.createVerticalPanel();//Create a panel which will hold all the elements 
var longUrlLabel = app.createLabel('lange E-Mail hier eingeben - Kurzlink wird dann per E-Mail zugesendet'); 
var longUrlBox = app.createTextBox().setName('longUrl') 
            .setText('http://www.'); 
var longUrlLabelInfo = app.createLabel().setId('shortUrlLabelInfo').setVisible(false); 
var button = app.createButton('Ausführen'); 


var handler = app.createServerClickHandler('buttonOnClickListener');//createHandler for the Button-onClick-Event. 
handler.addCallbackElement(panel); 
button.addClickHandler(handler); //Add this handler to the button 

//Add all the UI elements to the panel 
panel.add(longUrlLabel) 
    .add(longUrlBox) 
    .add(button); 
app.add(panel);//Add the panel to the application 
return app; 
} 

function buttonOnClickListener(eventInfo) { 
var app; 
Url.long = eventInfo.parameter.longUrl.toString(); 
Url.short = UrlShortener.Url.insert(UrlShortener.newUrl().setLongUrl(Url.short)); 
sendMail(); 
app = UiApp.getActiveApplication(); 
app.getElementById('longUrlLabelInfo').setVisible(true).setText(Url.short); 

return app; 
} 

function sendMail() { 
GmailApp.sendEmail(User.email, "UrlShortener", Url.long+" -> "+Url.short); 
} 

回答

0

嘗試這樣的:(編輯:我做了完善的UI一點,電子郵件佈局以獲得更清晰可讀值+英語翻譯;-)對此感到遺憾,但我的德語很差。 (;-)

var User = new Object(), 
Url = new Object(); 
User.email = Session.getActiveUser().getEmail(); 

function doGet() { 
    var app = UiApp.createApplication(); 
    var panel = app.createVerticalPanel().setStyleAttributes({'padding':'40px','backgroundColor':'#fafacc'}); 
    var longUrlLabel = app.createLabel('Enter the long url starting with http:// you will receive an email with the short url immediately.'); 
    var longUrlBox = app.createTextBox().setName('longUrl').addClickHandler(app.createClientHandler().forEventSource().setText('')) 
    .setText('http://').setWidth('500'); 
    var shortUrlLabel = app.createHTML().setId('shortUrlLabel').setVisible(false); 


    var handler = app.createServerHandler('buttonOnClickListener').addCallbackElement(panel); 
    var button = app.createButton('SUBMIT',handler).setStyleAttributes({'border-radius':'5px'}); 

    var grid = app.createGrid(8,1).setId('grid') 
    .setWidget(0,0,longUrlLabel) 
    .setWidget(2,0,longUrlBox) 
    .setWidget(4,0,button) 
    .setWidget(6,0,shortUrlLabel); 

    return app.add(panel.add(grid)); 
} 

function buttonOnClickListener(eventInfo) { 
    var app =UiApp.getActiveApplication(); 
    var toShorten = UrlShortener.newUrl().setLongUrl(eventInfo.parameter.longUrl); 
    var shortened = UrlShortener.Url.insert(toShorten); 
    Url.short = UrlShortener.Url.insert(toShorten); 
    Url.long = eventInfo.parameter.longUrl; 
    sendMail(); 
    app = UiApp.getActiveApplication(); 
    app.getElementById('shortUrlLabel').setVisible(true).setHTML('<li>Short url = <b>'+Url.short.id+'</b></li><li>Mail sent ...</li>'); 
    app.getElementById('grid').setWidget(7,0,app.createAnchor('test (with redirect warning)', Url.short.id)); 

    return app; 
} 

function sendMail() { 
    GmailApp.sendEmail(User.email, "UrlShortener", 'Long url (original) = '+Url.long+"\n\n\nShort url = "+Url.short.id); 
} 

enter image description here

+0

User.email沒有在點擊處理程序初始化。 Globals只在每次回調期間不活動,因此在它們之外初始化它們之前doget –

+0

這就是我做的... –

+0

作品,謝謝! :) *編輯* 問題:不是誰使用的應用程序的用戶收到的電子郵件,但我永諾。所以 GmailApp.sendEmail(User.email, 百達轉到腳本的管理員的電子郵件(我):(我不明白,因爲我發佈應用程序時設置了正確的設置。 – phip1611