簡體   English   中英

ServerClickHandler 導致“遇到錯誤:必需:必需”

[英]ServerClickHandler results in “Error encountered: Required: required”

我的 Google Apps 腳本中的 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 );
}

試試這樣:(編輯:我確實改進了 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);
}

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM