And*_*ntè 4 javascript forms post web-applications google-apps-script
我知道如何编写一个简单的应用程序脚本(一旦发布)提供一个简单的表单(名称和消息),将值推送到 Google 电子表格中。
我的问题是:如何从我的站点运行脚本?我可以构建一个 html 表单并在提交表单时写入电子表格吗?
有没有办法“创建”一个必须在电子表格中推送的值的链接?
我在哪里可以找到示例?
谢谢
编辑
商品代码
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();
}
}
Run Code Online (Sandbox Code Playgroud)
HTML(外部站点)
<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>
Run Code Online (Sandbox Code Playgroud)
我知道这不是有史以来最好的代码,但它有效。
当我单击按钮时,如何避免打开任何页面?
编辑:经过测试并有效。不要忘记部署为 Web 应用程序,授予表单访问权限以发布到脚本,并通过手动运行一次脚本来确保脚本可以访问目标工作表。
表单.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>
Run Code Online (Sandbox Code Playgroud)
代码.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);
}
Run Code Online (Sandbox Code Playgroud)