用于填写 HTML 表单的 Google 脚本

1 html javascript web-crawler google-sheets google-apps-script

我在谷歌脚本中创建了一个脚本,以获取电子表格并成功填写多个谷歌文档。我正在尝试将此逻辑应用于填写 HTML 表单,基本上,我们需要用户生成的信息(在我们的谷歌表中)来填写网页上的 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
} 
Run Code Online (Sandbox Code Playgroud)

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

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);
Run Code Online (Sandbox Code Playgroud)

Rob*_*Rob 5

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

我使用 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.
*/ 
}
Run Code Online (Sandbox Code Playgroud)