Google Apps Script run when SpreadSheet is change by another Script

寵の児 提交于 2019-12-11 10:19:40

问题


I'm trying to write a script that display row value in a SpreadSheet file using UiApp.

And this script will need to update the data whenever there is a change to the SpreadSheet. This would normally be simple with just Trigger onChange.

However, the SpreadSheet itself is also updated by another function, and it seem that onChange is not triggered when the changes made are caused by another AppScript.

Is there an alternative way to achieve what I described?

Code below, thanks

    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;
}

回答1:


The onChange() function will not have reference to the UI. For example, if there are multiple people who have the same UI open, which one should be updated ?

So, the solution to your problem is for your UI code to poll the spreadsheet regularly and update the UI if the spreadsheet has changed. To do so, you can use the undocumented addTimer() method. See this SO question & answer for a nice example



来源:https://stackoverflow.com/questions/17358808/google-apps-script-run-when-spreadsheet-is-change-by-another-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!