代码之家  ›  专栏  ›  技术社区  ›  mfaiz

使用设置中设置的定义参数运行脚本

  •  0
  • mfaiz  · 技术社区  · 8 年前

    我试图创建一些东西,当脚本运行时,它会读取其中设置的参数。

    例如,加载自定义边栏,用户可以在其中输入详细信息或参数来运行脚本。这些参数将一直保留,直到更改为止,但不应该每次运行使用这些参数的脚本时都需要这些参数。

    有点像设置菜单。

    我在一些插件中看到了类似的东西,有人能指出正确的方向来做这件事吗。

    我已经有脚本成功运行只需要一个用户界面,其中的参数可以输入和设置。如果可能的话,我希望避免从电子表格中的表格中阅读。

    编辑:

    我看到所有用户都可以使用getscriptproperty:

    到目前为止,我得到了更新(2): HTML:

    function showside(){
    
    SpreadsheetApp.getUi().showSidebar(HtmlService.createHtmlOutputFromFile('body'))
    }
    
    function setProperty(objectForm){
      PropertiesService.getScriptProperties().setProperties(
        {a1: objectForm.a1 , 
    		a2: objectForm.a2, 
    		p1: objectForm.p1,
    		p2: objectForm.p3})
      return 'Updated'
    }
    <!DOCTYPE html>
    <html>
      <head>
      <script>
      function readProperty(){
      
      var settings = PropertiesService.getScriptProperties(), keys = settings.getKeys()
      Logger.log('running')
      for (var i =0;i<keys.length;i++){
      document.getElementbyID(keys[i].toUpperCase()).value = settings.getProperty(keys[i])
    
      }
    }
    
    function handleFormSubmit(objectForm){
    google.script.run.setProperty(update).setProperty(objectForm)
    }
    
    function update(update){
            var div = document.getElementById('output');
            div.innerHTML = 'Updated!';
          }
    </script>
    
        <base target="_top">
      </head>
      <body onload="readProperty()">
      <form id="myForm" onsubmit="handleFormSubmit(this)">
      
      a1<input type="url" id="a1" value="" />
      a2<input type="url" id="a2" value="" />
      a3<input type="url" id="a3" value="" />
      P1<input type="text" id="P1" value="" />
      P2<input type="text" id="P2" value="" />
      <input type="submit" value="Submit" />
      </form>
      <div id="output"></div>
      </body>
      
      
    </html>
    1 回复  |  直到 8 年前
        1
  •  0
  •   Rajkumar Somasundaram    8 年前

    粗略的方法;从这里即兴创作;

    读取loadScript()中的注释;

    document.getElementById("formWithData").onsubmit = loadScript();
    
    function loadScript() {
       // get data from submitted form
       // this way script will re-run with data based on what u give in the form
    }
    <form id="formWithData">
    <input type="text" placeholder ="enter condition 1">
    <input type="text" placeholder ="enter condition 2">
    <input type="text" placeholder ="enter condition 3">
    </form>