gpt4 book ai didi

javascript - 从网页运行 Google App 脚本

转载 作者:行者123 更新时间:2023-11-29 17:54:33 26 4
gpt4 key购买 nike

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

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

有没有一种方法可以“创建”一个包含必须推送到电子表格中的值的链接?

我在哪里可以找到示例?

谢谢

编辑

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(外部站点)

<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>

我知道这不是有史以来最好的代码,但它确实有效。

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

最佳答案

编辑:测试和工作。不要忘记部署为 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>

代码.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);
}

关于javascript - 从网页运行 Google App 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40491631/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com