我已將 Google Apps 腳本設定為 WebApp,以從 HTML 表單(非 Google 表單)獲取表單資料并將結果插入 Google 表格。一切正常,但我正在嘗試獲取自定義登錄頁面而不是當前的 Apps 腳本頁面,這對最終用戶沒有用處。
我用這個作為參考:https ://github.com/levinunnink/html-form-to-google-sheet
這個 GitHub 指南的末尾有一些資訊,但它不是描述性的,我在這里或 GitHub 上找不到任何有用的東西。我知道一些 JS,但我不是專家,真的可以用手來解決這個問題。以下是我所擁有的,這是我最接近所有作業的地方。
這是我的 HTML。
<script>
function myFunction()
{ alert("The form was submitted successfully. Please press okay to continue...");
window.open("URL-to-thanks-page", "_top");
}
</script>
<form class="googleform" name="googleform" id="googleform" target="_top" method="POST" onsubmit="myFunction()" action="MY-GOOGLE-APPS-SCRIPTS-URL">
<!--FORM STUFF-->
<button type="submit" class="btn-default btn" name="submit" access="false" style="default" id="submit">Submit</button>
</form>
這是我的 Code.gs
const sheetName = 'RSVP'
const scriptProp = PropertiesService.getScriptProperties()
function intialSetup () {
const activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet()
scriptProp.setProperty('key', activeSpreadsheet.getId())
}
function doPost (e) {
const lock = LockService.getScriptLock()
lock.tryLock(10000)
try {
const doc = SpreadsheetApp.openById(scriptProp.getProperty('key'))
const sheet = doc.getSheetByName(sheetName)
const headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0]
const nextRow = sheet.getLastRow() 1
const newRow = headers.map(function(header) {
return header === 'Date' ? new Date() : e.parameter[header]
})
sheet.getRange(nextRow, 1, 1, newRow.length).setValues([newRow])
return ContentService
.createTextOutput(JSON.stringify({ 'result': 'success', 'row': nextRow }))
.setMimeType(ContentService.MimeType.JSON)
}
catch (e) {
return ContentService
.createTextOutput(JSON.stringify({ 'result': 'error', 'error': e }))
.setMimeType(ContentService.MimeType.JSON)
}
finally {
lock.releaseLock()
}
}
任何幫助將不勝感激。
尊敬,
uj5u.com熱心網友回復:
我實際上想通了這一點。我使用了原始指南上的代碼并進行了修改。在我的 HTML 中,我洗掉了上<script>一部分并將其更改為以下內容:
<script>
window.addEventListener("load", function() {
const form = document.getElementById('googleform');
form.addEventListener("submit", function(e) {
e.preventDefault();
const data = new FormData(form);
const action = e.target.action;
fetch(action, {
method: 'POST',
body: data,
})
.then(() => {
window.location.href = 'URL of thank you page';
})
});
});
</script>
這解決了問題,現在一切正常。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/419486.html
標籤:
上一篇:GoogleApps腳本。如何將我的電子表格ID指定為“Sheet3”?
下一篇:自定義列選擇谷歌腳本
