Google Script รับค่ากล่องข้อความ UI

ฉันได้สร้าง GUI แบบง่ายพร้อมกล่องข้อความ 2 กล่องและปุ่ม 1 ปุ่ม ตัวจัดการปุ่มมีลักษณะดังนี้

function handleButton1(e) 
{
    var app = UiApp.getActiveApplication();
    var v1 = e.parameter.TextBox1;
    var v2 = e.parameter.TextBox2;
    Logger.log(v1);
    app.getElementById("TextBox1").setText(v2);
    app.getElementById("TextBox2").setText(v1);
    return app;
}

เมื่อฉันเรียกใช้แอป ค่ากล่องข้อความคือ TextBox1 และ TextBox2 เมื่อกดปุ่ม ค่าของกล่องข้อความที่แสดงทั้งสองค่าจะไม่ได้ถูกกำหนดไว้ ฉันผิดตรงไหน..


person user2035041    schedule 02.02.2013    source แหล่งที่มา


คำตอบ (1)


ด้วยตัวจัดการการคลิกฝั่งเซิร์ฟเวอร์ คุณจะต้องรวมค่าไว้ในเหตุการณ์ตัวจัดการอย่างชัดเจนโดยใช้ .addCallbackElement() หากคุณทำเช่นนั้น ค่าปัจจุบันขององค์ประกอบที่มีชื่อที่คุณเพิ่มจะรวมอยู่ในเหตุการณ์ที่ส่งไปยังตัวจัดการของคุณ

เนื่องจากคุณเห็น undefined จึงเป็นไปได้ว่าคุณไม่ได้เพิ่มการโทรกลับ คุณควรมีสิ่งนี้ในคำจำกัดความ UI ของคุณ:

var handler = app.createServerHandler('handleButton1')
                 .addCallbackElement(textBox1)
                 .addCallbackElement(textBox2);
button.addClickHandler(handler);

ชื่อขององค์ประกอบจะถูกใช้เพื่อติดป้ายกำกับค่าโทรกลับ (.setName()) ในขณะที่รหัสจะถูกใช้เพื่อเข้าถึงองค์ประกอบในตัวจัดการของคุณ (.setId())

นี่คือสคริปต์เวอร์ชันที่ใช้งานได้:

function doGet() {
  var app = UiApp.createApplication();

  var textBox1 = app.createTextBox().setName("TextBox1").setId("TextBox1");
  var textBox2 = app.createTextBox().setName("TextBox2").setId("TextBox2");
  var button = app.createButton('Swap Contents');
  app.add(textBox1).add(textBox2).add(button);

  var handler = app.createServerHandler('handleButton1')
                   .addCallbackElement(textBox1)
                   .addCallbackElement(textBox2);
  button.addClickHandler(handler);

  return app;
}

function handleButton1(e) 
{
  //Logger.log(JSON.stringify(e));
  var app = UiApp.getActiveApplication();
  var v1 = e.parameter.TextBox1;
  var v2 = e.parameter.TextBox2;
  app.getElementById("TextBox1").setText(v2);
  app.getElementById("TextBox2").setText(v1);
  return app;
}
person Mogsdad    schedule 17.07.2013