【问题标题】:Google Script to Fill out HTML Form用于填写 HTML 表单的 Google 脚本
【发布时间】:2020-05-21 22:41:26
【问题描述】:

我在谷歌脚本中创建了一个脚本,以获取电子表格并成功填写多个谷歌文档。我正在尝试将此逻辑应用于填写 HTML 表单,基本上,我们需要用户生成的信息(在我们的 google 表格中)来填写网页上的 HTML 表单。

如何让以下函数不仅打开而且写入数据?

这就是我所在的位置(仅使用示例网页):

function testNew(){
  var js = " \
    <script> \
      window.open('https://colorlib.com/etc/cf/ContactFrom_v1/index.html', '_blank', 'width=800, height=600'); \
      google.script.host.close(); \
    </script> \
  ";
  var html = HtmlService.createHtmlOutput(js)
    .setHeight(10)
    .setWidth(100);
  SpreadsheetApp.getUi().showModalDialog(html, 'Now loading.'); // If you use this on Spreadsheet
//  DocumentApp.getUi().showModalDialog(html, 'Now loading.'); //  If you use this on Document
//  SlidesApp.getUi().showModalDialog(html, 'Now loading.'); //  If you use this on Slides
} 

这是我对谷歌文档所做的一个例子,试图在表单中复制:

function myFunction() {
      var data = Sheets.Spreadsheets.Values.get('google sheet ID HERE', 'A2:R300');
      // google doc template id, already got deal memo
      var templateId = 'google doc ID HERE';
      // loop through the values "i" is looping the rows, "#" is the column example: 0=a,1=b   
      for (var i = 0; i < data.values.length; i++) {
        var date = data.values[i][0];
        var email = data.values[i][1];
        // grab the google doc template, create a copy, and generate the new id
        var documentId = DriveApp.getFileById(templateId).makeCopy().getId();
        // change the name of the new file
        DriveApp.getFileById(documentId).setName(companyName+ '-' + projectName+ '-' + 'Insurance Report');
        // get the document body as a variable
        var body = DocumentApp.openById(documentId).getBody();
        // replace values with google sheet data
        body.replaceText('##Date##', date);
        body.replaceText('##Email##', email);

【问题讨论】:

  • html 表单在哪里托管?
  • 是公司网站吗?
  • 是的,基本上是在尝试创建一个系统来为数百个客户填写表单,因为他们不会集成 CSV 上传器。
  • 您为什么不想使用您当前的网站数据库。
  • 这不是我的网站,如果可以的话完全可以。基本上,该表格来自另一家需要我们填写(并且不接受 CSV)的公司,我们的数据库中有所有答案,并正在尝试使用谷歌脚本或其他方法来获取这些 csv 并填写他们的在线表格,每次我们得到一个新客户。

标签: javascript html google-apps-script google-sheets web-crawler


【解决方案1】:

我编写了许多与许多第三方表单和网站交互的函数。不要让它愚弄你,表单就是一种人类可读的“POST”到 url 的方式。更简单的方法是在 Google Apps 脚本中使用 UrlFetchApp.fetch(url, options) 函数。

我使用 Chrome,但其他浏览器也有此功能:

  1. 打开 Chrome,导航到表单,然后按 F12 打开开发人员窗口。
  2. 点击“网络”
  3. 手动填写表格,然后查看“网络”窗口中的流量并找到您的浏览器发送到该站点的 POST,
  4. 突出显示后,您将看到“标题”、“预览”、“响应”的其他几个选项卡
  5. 在“标题”选项卡中,滚动到底部,它将显示请求的外观。截屏并通过 UrlFetchApp.fetch() 将这些变量作为“有效负载”发送到网站,并格式化如下函数。
  6. 查看同一“标题”选项卡顶部的“请求 URL”,您将使用它作为下面的 URL:
function senddatatoform() {

var url = 'http://theurlthattheformpoststohere.com'; // this is the 'request url' as shown in your browser (not always the url of the form).  

var payload = {
     datapoint1: datapoint1value,
     datapoint2: datapoint2value,
     ... //continue with all required post data   
     }

var options = {
    method: "POST",
    payload: payload
    }

var response = UrlFetchApp.fetch(url,options);
/*this is where what you need to do next might vary -- if you're looking for a 'success' page, you might write some code here to verify that the http response is correct based on your needs.
*/ 
}

【讨论】:

  • 似乎是个好主意。但他的客户将如何利用这一点呢?
  • OP 说他在他的谷歌表格中有用户生成的信息,假设它来自在其他地方填写的表格。似乎他们想要一些可以利用客户数据自动执行内部任务的东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多