使用网络脚本应用在Google工作表中写入数据 [英] write data in google sheet using a web script app

查看:77
本文介绍了使用网络脚本应用在Google工作表中写入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一件看似非常简单的事情,但我不明白: 我想创建一个Google Web应用程序,让用户写一些文本,然后当他单击按钮时,文本就会进入Google表格.

I am trying to do Something apparently very simple, but I don't get it : I would like to create a google web app, in wich the user write some text, and when he clicks on a button, the text goes into a Google Sheet.

然后,我也想做个矛盾的事情:当用户单击Google Web应用程序的另一个按钮时,他可以看到google工作表单元格中写的内容

Then, I would like to do also the contrairy : when the user click on a other button of the google web app, he can see what is written in a cell of the google sheet

我最大的问题是我只是不知道如何在webb应用程序和工作表之间建立连接,以及如何使用它.

My biggest problem is that i just don't know how to do the connexion between the webb app and the sheet, and how to use it.

你能帮我吗? 谢谢

推荐答案

这是我修改为可以使用的搜索脚本.输入文本,然后按提交,然后转到电子表格.

Here is a search script I modified to work. Input text press submit and the words goto Spreadsheet.

您需要部署为Web应用程序.

You need to deploy as web app.

CODE.GS

function doGet(e) { // main function
  var template = HtmlService.createTemplateFromFile('index.html'); 
  return template.evaluate().setTitle('Search Drive').setSandboxMode(HtmlService.SandboxMode.IFRAME);
}

// Process the form
function processForm(searchTerm) {
  var resultToReturn;
  resultToReturn  = TextToSheet(searchTerm);
  return resultToReturn; // return the results
}

function TextToSheet(searchTerm) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  SpreadsheetApp.getActiveSheet().getRange('A1').setValue(searchTerm);
 
}

INDEX.HTML

INDEX.HTML

<html>
  <head>
    <base target="_top">
    <script>
      function displayMessage() {
        var searchTerm;
        searchTerm = document.getElementById('idSrchTerm').value;
        console.log('searchTerm: ' + searchTerm );
         google.script.run.withSuccessHandler(handleResults).processForm(searchTerm.replace("'","\'")); 
       }
         
        
      function handleResults(results){
         var length=results.length; // total elements of results
       }


    </script>
  </head>
  <body><center><br/>
   Input word to SpreadSheet: <input type="text" id="idSrchTerm" name="search">
    <input type="button" value="Submit" name="submitButton" onclick="displayMessage()"/>
</center>
  </body>
</html>

这篇关于使用网络脚本应用在Google工作表中写入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆