【问题标题】:Get textarea value from google document ui into gs code从谷歌文档ui中获取textarea值到gs代码中
【发布时间】:2020-10-26 13:20:05
【问题描述】:

我想将 gs 代码中的 2 个变量的值设置为 HTML 文件的 2 个文本区域中的文本值。

sidebar.html:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <input type="button" value="go go go!" onclick="google.script.run.mainFunction();"/> 
    <strong>Text Area 1</strong>
    <textarea id="textarea1" rows="2" cols="35">Text1</textarea>
    <strong>Text Area 2</strong>
    <textarea id="textarea2" rows="3" cols="35">Text2</textarea>
  </body>
</html>

code.gs:

mainFunction() {
  var textArea1Value = ???; // should be "Text1"/user's input
  var textArea2Value = ???; // should be "Text2"/user's input
  // some code
}

我如何实现这一目标? (我应该写什么而不是gs代码中的“???”?)

我已尝试寻找解决方案,但由于答案过于具体,我不确定如何实施我的发现

谢谢

【问题讨论】:

    标签: html user-interface google-apps-script google-docs


    【解决方案1】:

    您需要将值作为参数从客户端传递到服务器端

    为此,请在 Javascript 代码部分中通过 id 获取 textareas 并将它们的值传递给 google.script.run

    对您的代码进行示例修改:

    sidebar.html:

        <input type="button" value="go go go!" onclick="myJSFunction()"/> 
        <strong>Text Area 1</strong>
        <textarea id="textarea1" rows="2" cols="35">Text1</textarea>
        <strong>Text Area 2</strong>
        <textarea id="textarea2" rows="3" cols="35">Text2</textarea>
        <script>
        function myJSFunction(){
          var text1 = document.getElementById("textarea1").value;
          var text2 = document.getElementById("textarea2").value;
          google.script.run.mainFunction(text1, text2);
        }
        </script>
    

    code.gs:

    function mainFunction(text1, text2) {
      var textArea1Value = text1; 
      var textArea2Value = text2;
      // some code
    }
    

    【讨论】:

    • 太棒了!这中间步骤正是我所缺乏的。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-01
    • 1970-01-01
    • 2023-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多