是否有方法从PowerShell连接到在谷歌应用程序脚本中创建的web应用程序进行web调用?
当我在普通网站上运行请求时,我会收到包含Forms[]、Images[]、InputFields[]等的返回信息。但是,当我试图在https://script.google.com/a/macros/web应用程序所有这些字段都是空白的,我只能看到一个名为el的变量链接到名为sandboxFrame的字段。
该应用程序是一个简单的上传网站到我的一个谷歌文件夹,当我在浏览器中时,一切都正常。我正试图通过PowerShell脚本实现流程自动化
HTML文件
<!DOCTYPE html>
<html>
<body>
<input name="file" id="files" type="file" multiple>
<input type='button' value='Upload' onclick='getFiles()'>
</body>
<script>
function getFiles() {
const f = document.getElementById('files');
[...f.files].forEach((file, i) => {
const fr = new FileReader();
fr.onload = (e) => {
const data = e.target.result.split(",");
const obj = {fileName: f.files[i].name, mimeType: data[0].match(/:(w.+);/)[1], data: data[1]};
google.script.run.withSuccessHandler((id) => {
console.log(id);
}).saveFile(obj);
}
fr.readAsDataURL(file);
});
}
</script>
</html>
GS脚本
function saveFile(obj) {
var folder = DriveApp.getFolderById('1w586veZcOZN_NnB90jaTZ12DF-jP005u');
var blob = Utilities.newBlob(Utilities.base64Decode(obj.data), obj.mimeType, obj.fileName);
return folder.createFile(blob).getId();
}
您需要利用doPost
函数
我想您已经知道doGet
函数,但还有另一个函数可以作为名为doPost
的web应用程序的一部分使用。这允许您使用powershell中的以下内容发布数据:
Invoke-WebRequest https://script.google.com/a/macros/[SCRIPTID]?[QUERYSTRING] -Method POST
其中[QUERYSTRING]
类似于:
name=bartosz&stack=BartoszWolas&reputation=1000
然后在web应用程序端的doPost
中,您将编写这样的函数:
function doPost(e) {
const name = e.parameter.name; // bartosz
const stackAlias = e.parameter.stack; // BartoszWolas
const reputation = e.parameter.reputation; // 1000
}
参考
- 网络应用程序