如何构建扩展以使用应用程序脚本将HTML表单的值保存到电子表格

时间:2015-10-15 17:42:45

标签: javascript google-chrome-extension google-apps-script

我正在尝试构建一个扩展程序,使用谷歌应用程序脚本将HTML表单的值保存到电子表格中。

当我通过应用程序脚本发布为Web应用程序时,我能够保存 但是当我尝试使用不起作用的扩展时。

在提交表单时,如何调用应用脚本网址以便从扩展程序将值发布到电子表格中?

HTML:

<form name="input">
    <!-- fieldset draws a border around all the input elements -->
    <fieldset>
        <!-- There are form attributes, like type, name, id -->
        <input type="text" value="Manufacturer"
            class="ss-q-short" id="id_Maker" dir="auto" aria-required="true"
            title=""> <br>
        <br>

        <br> <input type="button" value="Save Input"
            onclick="WriteInput()"> <input type="reset" value="Reset">

    </fieldset>
</form>

JS文件:

<script>
     function WriteInput() {
     alert("hello");
        var d=new Date();
        var daMonth = d.getMonth() + 1;
        var ToInputStrng = (daMonth + "/" + d.getDate() + "/" + d.getFullYear()  + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds());
         alert(ToInputStrng);
        var strngTwo = document.getElementById('id_Maker').value;

        alert(strngTwo);

        google.script.run.withFailureHandler(onFailure)
           .withSuccessHandler(onSuccess)
           .AddToSheet(ToInputStrng, strngTwo);
    }
</script>

app脚本:

function doGet(){
    return HtmlService.createHtmlOutputFromFile('OfferedInput')
            .setSandboxMode(HtmlService.SandboxMode.NATIVE);
}

function AddToSheet(InputData, InputTwo){
    var sheet = SpreadsheetApp.openById("Put the spreadsheet ID here");
    return sheet.appendRow([InputData, InputTwo]);
} 

1 个答案:

答案 0 :(得分:0)

放一个&#34; scriptlet&#34;进入你的&#34; OfferedInput&#34; HTML文件。 &#34; scriptlet&#34;是<!DOCTYPE html><html>。请务必在HTML中使用<body><!DOCTYPE html> <html> <head> <?!= include("JS"); ?> </head> <body> <form name="input"> <!-- fieldset draws a border around all the input elements --> <fieldset> <!-- There are form attributes, like type, name, id --> <input type="text" value="Manufacturer" class="ss-q-short" id="id_Maker" dir="auto" aria-required="true" title=""> <br> <br> <br> <input type="button" value="Save Input" onclick="WriteInput()"> <input type="reset" value="Reset"> </fieldset> </form> </body> </html> 标记:

createTemplateFromFile()

scriplet使HTML文件成为&#34;模板&#34;。现在,您必须使用doGet()中的include()方法。你需要一个function doGet() { var template = HtmlService.createTemplateFromFile('OfferedInput'); // Build and return HTML in IFRAME sandbox mode. return template.evaluate() .setTitle('Web App Window Title') .setSandboxMode(HtmlService.SandboxMode.IFRAME); }; function include(filename) { //Logger.log('filename: ' + filename) return HtmlService.createHtmlOutputFromFile(filename).getContent(); }; 函数。

AppDeployPreHook/03deploy.py] : Activity execution failed, because: You are using pip version 7.0.3, however version 7.1.2 is available.
      You should consider upgrading via the 'pip install --upgrade pip' command.
      Exception:
      Traceback (most recent call last):
        File "/opt/python/run/venv/local/lib/python2.7/site-packages/pip/basecommand.py", line 223, in main
          status = self.run(options, args)
        File "/opt/python/run/venv/local/lib/python2.7/site-packages/pip/commands/install.py", line 268, in run
          wheel_cache
        File "/opt/python/run/venv/local/lib/python2.7/site-packages/pip/basecommand.py", line 287, in populate_requirement_set
          wheel_cache=wheel_cache):
        File "/opt/python/run/venv/local/lib/python2.7/site-packages/pip/req/req_file.py", line 86, in parse_requirements
          for req in req_iter:
        File "/opt/python/run/venv/local/lib/python2.7/site-packages/pip/req/req_file.py", line 130, in process_line
          wheel_cache=wheel_cache
        File "/opt/python/run/venv/local/lib/python2.7/site-packages/pip/req/req_install.py", line 207, in from_line
          wheel_cache=wheel_cache)
        File "/opt/python/run/venv/local/lib/python2.7/site-packages/pip/req/req_install.py", line 66, in __init__
          req = pkg_resources.Requirement.parse(req)
        File "/opt/python/run/venv/local/lib/python2.7/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2960, in parse
          reqs = list(parse_requirements(s))
        File "/opt/python/run/venv/local/lib/python2.7/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2904, in parse_requirements
          "version spec")
        File "/opt/python/run/venv/local/lib/python2.7/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2869, in scan_list
          raise ValueError(msg, line, "at", line[p:])
      ValueError: ('Expected version spec in', '_license=1.1=py27_0', 'at', '=1.1=py27_0')

  2015-10-16 01:37:43,439 ERROR    Error installing dependencies: Command '/opt/python/run/venv/bin/pip install -r /opt/python/ondeck/app/requirements.txt' returned non-zero exit status 2
  Traceback (most recent call last):
    File "/opt/elasticbeanstalk/hooks/appdeploy/pre/03deploy.py", line 22, in main
      install_dependencies()
    File "/opt/elasticbeanstalk/hooks/appdeploy/pre/03deploy.py", line 18, in install_dependencies
      check_call('%s install -r %s' % (os.path.join(APP_VIRTUAL_ENV, 'bin', 'pip'), requirements_file), shell=True)
    File "/usr/lib64/python2.7/subprocess.py", line 540, in check_call
      raise CalledProcessError(retcode, cmd)
  CalledProcessError: Command '/opt/python/run/venv/bin/pip install -r /opt/python/ondeck/app/requirements.txt' returned non-zero exit status 2 (ElasticBeanstalk::ExternalInvocationError)
caused by: You are using pip version 7.0.3, however version 7.1.2 is available.
  You should consider upgrading via the 'pip install --upgrade pip' command.
  Exception:
  Traceback (most recent call last):
    File "/opt/python/run/venv/local/lib/python2.7/site-packages/pip/basecommand.py", line 223, in main
      status = self.run(options, args)
    File "/opt/python/run/venv/local/lib/python2.7/site-packages/pip/commands/install.py", line 268, in run
      wheel_cache
    File "/opt/python/run/venv/local/lib/python2.7/site-packages/pip/basecommand.py", line 287, in populate_requirement_set
      wheel_cache=wheel_cache):
    File "/opt/python/run/venv/local/lib/python2.7/site-packages/pip/req/req_file.py", line 86, in parse_requirements
      for req in req_iter:
    File "/opt/python/run/venv/local/lib/python2.7/site-packages/pip/req/req_file.py", line 130, in process_line
      wheel_cache=wheel_cache
    File "/opt/python/run/venv/local/lib/python2.7/site-packages/pip/req/req_install.py", line 207, in from_line
      wheel_cache=wheel_cache)
    File "/opt/python/run/venv/local/lib/python2.7/site-packages/pip/req/req_install.py", line 66, in __init__
      req = pkg_resources.Requirement.parse(req)
    File "/opt/python/run/venv/local/lib/python2.7/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2960, in parse
      reqs = list(parse_requirements(s))
    File "/opt/python/run/venv/local/lib/python2.7/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2904, in parse_requirements
      "version spec")
    File "/opt/python/run/venv/local/lib/python2.7/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2869, in scan_list
      raise ValueError(msg, line, "at", line[p:])
  ValueError: ('Expected version spec in', '_license=1.1=py27_0', 'at', '=1.1=py27_0')

  2015-10-16 01:37:43,439 ERROR    Error installing dependencies: Command '/opt/python/run/venv/bin/pip install -r /opt/python/ondeck/app/requirements.txt' returned non-zero exit status 2
  Traceback (most recent call last):
    File "/opt/elasticbeanstalk/hooks/appdeploy/pre/03deploy.py", line 22, in main
      install_dependencies()
    File "/opt/elasticbeanstalk/hooks/appdeploy/pre/03deploy.py", line 18, in install_dependencies
      check_call('%s install -r %s' % (os.path.join(APP_VIRTUAL_ENV, 'bin', 'pip'), requirements_file), shell=True)
    File "/usr/lib64/python2.7/subprocess.py", line 540, in check_call
      raise CalledProcessError(retcode, cmd)
  CalledProcessError: Command '/opt/python/run/venv/bin/pip install -r /opt/python/ondeck/app/requirements.txt' returned non-zero exit status 2 (Executor::NonZeroExitStatus)

将沙箱模式设置为IFRAME,NATIVE被视为已弃用。