[英]Return Value from function in gs file to javascript file (HTML)
我需要將值 (idValues) 從 gs 文件返回到 javascript 文件。
GS文件
function getMachineDB() {
var url = "https://docs.google.com/spreadsheets/d/1ZI16KqM7Uy_ZDTsTUFYFRSXS-DBkRdigp-3SiIy-
CPA/edit#gid=693962232";
var ss = SpreadsheetApp.openByUrl(url);
var sheet = ss.getSheetByName("Equiplist");
var idValues = sheet.getRange(2,4,sheet.getLastRow()).getValues();
return idValues;
}
Javascript文件
function regExp(machinenoNum){
google.script.run.withSuccessHandler(function(idValues) {
var vmachineno = "/(" + idValues.join("|") + ")/";
return (vmachineno.test(machinenoNum));
}).getMachineDB();
}
function isValid() {
var x = document.getElementById("machineno");
if (regExp(x.value)) {
return true
} else {
machineno.setCustomValidity('nomor mesin tidak terdaftar');
return false
}
}
HTML文件
<input type="text" id="machineno" class="form-input" name="entry.1002184227" placeholder="xx-xxx-xx" onChange="return isValid()" oninput="this.setCustomValidity('')" required/>
這些代碼將文本框表單中的值與電子表格中的一些數據進行比較。 如何從 GS 文件獲取 idValue 的值到 javascript 文件? 謝謝你
為了重現您的行為,我創建了一個新項目進行測試。 我還使用電子表格中的值 1-9 進行驗證。
當前運行 Web 應用程序時,它會在控制台中顯示以下內容:
這與此代碼有關:
function regExp(machinenoNum){
google.script.run.withSuccessHandler(function(idValues) {
var vmachineno = "/(" + idValues.join("|") + ")/";
return (vmachineno.test(machinenoNum));
}).getMachineDB();
}
如果您在此代碼中添加一些console.log
:
function regExp(machinenoNum){
google.script.run.withSuccessHandler(function(idValues) {
console.log(idValues)
var vmachineno = "/(" + idValues.join("|") + ")/";
console.log(vmachineno)
return (vmachineno.test(machinenoNum));
}).getMachineDB();
}
你得到:
idValues
是[[1],[2],[3],[4],[5],[6],[7],[8],[9]]
vmachineno
是"/(1|2|3|4|5|6|7|8|9|)/"
vmachineno
只是一個字符串,它沒有任何稱為test
方法。 所以這就是你在這里出錯的原因。
在電子表格中找不到值也沒有附加任何操作,因此即使錯誤得到解決,這仍然不會警告用戶輸入無效。
function regExp(machinenoNum){
google.script.run.withSuccessHandler(function(idValues) {
// This just converts the `idValues` into simple array of strings.
const values = idValues.map(val => `${val[0]}`)
// Checking if the values from SS contain the machinenoNum value
// If this is true, function returns and does nothing.
if (values.includes(machinenoNum)) return true
// If the above check is false, then this will make an alert in the browser window.
alert("invalid input!")
})
.getMachineDB();
}
因此,當您輸入電子表格中的值時,什么也不會發生。 如果該值不在電子表格中,則會得到以下信息:
11
然后按 ENTER。onChange="return isValid()"
運行。isValid()
將從文本框中獲取值var x = document.getElementById("machineno");
if (regExp(x.value)) {
regExp
正在運行。x.value
是否包含在那里。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.