这里有一个这样的脚本示例:How do I use Google Apps Script to change a CSS page to one with inline styles?。
这是一个不同的应用程序,但方法适用。这是一个可以帮助您入门的骨架函数。
payload 对象应包含您要模拟的表单数据的名称/值对。 url 应更改为与您提交表单的站点相匹配。通过调用UrlFetchApp.fetch() 发出POST 请求后,您应该能够使用来自Does Google Apps Script have something like getElementById? 的getElementByVal() 实用程序解析响应。
function postForm() {
// Generate a POST request with form data.
var payload =
{
"name" : 'Anonymous Person', // Replace with relevant name / value pairs
"town" : "Springfield"
};
// Because payload is a JavaScript object, it will be interpreted as
// an HTML form. (We do not need to specify contentType; it will
// automatically default to either 'application/x-www-form-urlencoded'
// or 'multipart/form-data')
var options =
{
"method" : "post",
"payload" : payload,
"muteHttpExceptions" : true
};
var url = "http://form.somewhere.com"; // Replace as appropriate
var response = UrlFetchApp.fetch(url, options);
// Put the receieved xml response into XMLdocument format
var xml = response.getContentText();
var doc = XmlService.parse(xml);
// Extract the details you want...
var someData = getElementByVal( doc, 'textarea', 'name', 'text' );
...
}