从网页运行Google App脚本 [英] Run Google App Script from web page

查看:57
本文介绍了从网页运行Google App脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何编写一个简单的应用程序脚本(一旦发布),该脚本将提供一种简单的形式(名称和消息),以将值推入Google电子表格中.

I know how to write a simple app script that (once published) provide a simple form (name and message) that push values in a Google Spreadsheet.

我的问题是:如何从我的网站运行脚本?我可以构建html表单吗?提交表单时,可以在电子表格中写吗?

My question is: how can I run the script from my site? Can I build an html form and when I submit the form writes in a spreadsheet?

是否可以使用必须在电子表格中推送的值来创建"链接?

Is there a way to "create" a link with values that must be pushed in the spreadsheet?

在哪里可以找到示例?

谢谢

编辑

GS代码

var logSheetId = "[SHEETID]"; // Drive ID of log spreadsheet

function doGet(e){
var nome;
var messaggio;

nome = e.parameter.stringaNome;
messaggio=e.parameter.stringaMessaggio;
scriviSpreadsheet(nome,messaggio);
}

function scriviSpreadsheet(nome, messaggio) {
try {

// Open the log and record the new file name, URL and name from form
var ss = SpreadsheetApp.openById(logSheetId);
var sheet = ss.getSheets()[0];
sheet.appendRow([nome, messaggio]);

// Return the new file Drive URL so it can be put in the web app output
//return file.getUrl();
} catch (error) {
return error.toString();
}
}

HTML(外部网站)

HTML (external site)

<form id="myForm">
<input type="text" id="nome" name="nome" placeholder="Il tuo nome"/>
<input type="text" id="messaggio" name="messaggio"/>
<input type="button" value="Submit"
  onclick="scrivi()" />
</form>

<script>
function scrivi(){
  var nome= document.getElementById("nome").value;
  var messaggio = document.getElementById("messaggio").value;

  var location = "https://script.google.com/macros/s/[MYSCRIPTID]/exec?stringaNome=" + nome + "&stringaMessaggio=" + messaggio;
  window.location = location;

}

function onFailure(error) {
    alert(error.message);
}
</script>

我知道这不是有史以来最好的代码,但是它可以工作.

I know it's not the best code ever but it works.

如何避免单击按钮时打开任何页面?

How can I avoid that any page open when I click the button?

推荐答案

经过测试,可以正常工作.不要忘记部署为Web应用程序,为表单提供发布到脚本的访问权限,并通过手动运行一次脚本来确保脚本可以访问目标工作表.

Tested and works. Don't forget to deploy as web app, give the form access to post to the script, and ensure the script has access to the target sheet by running the script manually once.

form.html:

form.html:

<form action="https://script.google.com/macros/s/[SCRIPT ID]/exec" method="post">
  Input One:<br>
  <input type="text" name="inputOne"><br>
  Input Two:<br>
  <input type="text" name="inputTwo"><br>
  Input Three:<br>
  <input type="text" name="inputThree"><br>
  Input Four:<br>
  <input type="text" name="inputFour"><br>
  Input Five:<br>
  <input type="text" name="inputFive"><br>
  <input type="submit" value="Submit">
</form>

Code.gs:

function doPost(e) {

  var ss = SpreadsheetApp.openById("SHEET ID");
  var sheet = ss.getSheetByName("Sheet1");
  var sheet_counters = ss.getSheetByName("Counters");

  var id = sheet_counters.getRange("A2").getValue()+1;
  var timeZone = ss.getSpreadsheetTimeZone();
  var timestamp = Utilities.formatDate(new Date(), timeZone, "dd/MM/yyyy HH:mm:ss");

  sheet.appendRow([
    id,
    timestamp,
    e.parameter.inputOne,
    e.parameter.inputTwo,
    e.parameter.inputThree,
    e.parameter.inputFour,
    e.parameter.inputFive]);

  sheet_counters.getRange("A2").setValue(id);
}

这篇关于从网页运行Google App脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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