1

我正在尝试编写一个脚本,该脚本使用UiApp在SpreadSheet文件中显示行值。当SpreadSheet被另一个脚本更改时运行Google Apps脚本

这个脚本需要在SpreadSheet发生变化时更新数据。只需触发onChange即可。

但是,SpreadSheet本身也会被另一个函数更新,并且看起来当更改是由另一个AppScript引起时,onChange不会被触发。

是否有另一种方法来实现我所描述的?

下面的代码,感谢

var ss = SpreadsheetApp.openById("tqwJk280I1O_yW5oNX9nLQA"); 

function doGet() { 
    var app = UiApp.createApplication(); 
    var masPanel = app.createFlowPanel().setId("panel"); 
    var number = parseInt(ss.getRange("A1").getValue()); 
    masPanel.add(app.createLabel(number)); 
    masPanel.add(app.createFlowPanel().add(app.createFormPanel() 
      .add(app.createFlowPanel() 
      .add(app.createSubmitButton("Plus!"))))); 

    app.add(masPanel); 

    return app; 
} 

function doPost() 
{ 
    var num = parseInt(ss.getRange("A1").getValue()) + 1; 
    ss.getRange("A1").setValue(num); 
} 

function onChange() 
{ 
    ss.getRange("A2").setValue("hahaha"); 
    var app = UiApp.createApplication(); 
    app.remove(app.getElementById("panel")) 
    var masPanel = app.createFlowPanel().setId("panel"); 
    var number = parseInt(ss.getRange("A1").getValue()); 
    masPanel.add(app.createLabel(number)); 
    masPanel.add(app.createFlowPanel().add(app.createFormPanel() 
      .add(app.createFlowPanel() 
      .add(app.createSubmitButton("Plus!"))))); 

    app.add(masPanel); 

    return app; 
} 

回答

2

onChange()功能不会有参考UI。例如,如果有多个拥有相同用户界面的人打开,应该更新哪一个?

因此,解决您的问题的方法是让您的UI代码定期轮询电子表格,并在电子表格发生变化时更新UI。为此,您可以使用未记录的addTimer()方法。 看到这个SO question & answer为一个很好的例子

+0

非常感谢,斯里克,这个功能真的很有帮助。有关于这些功能的页面或论坛吗? – MikeNQ

+0

正如我所说的,addTimer还没有记录:)希望Google能尽快正式上线 – Srik

相关问题