Google Apps Script with SuccessHandler() 无法按预期工作



我正在学习Apps Script,我感到非常沮丧,因为我看不出我的方法有什么问题。

根据文档,当从html文件脚本中运行google.script.run.withSuccessHandler(successFunc).myFunction()时,无论myFunction返回什么都应该作为successFunc的参数可用,或者不应该吗?我现在很怀疑,因为我似乎不能使它工作。

这是我的HTML:

<body>
<form>
<label for="fn">Nombre</label>
<input id="fn" name="fn" type="text">
<label for="ln">Apellido/s</label>
<input id="ln" name="ln" type="text">
<button type="button" id="search">Buscar</button>
<label for="found">Resultado</label>
<input type="text" name="found" id="found" disabled>
<button type="submit" id="setClient">Continuar</button>
</form>
<script>
const fn = document.getElementById('fn').value;
const ln = document.getElementById('ln').value;
const search = document.getElementById('search');
const found = document.getElementById('found');
const setClient = document.getElementById('setClient');
let clientId; // The ID of the client in the database;
let data;
search.addEventListener("click", () => {
google.script.run.withSuccessHandler(addClient).searchDb(fn, ln);
});
function addClient(data) {
alert(data); // returns null
if (!data || data === null) throw "No data found"; // don't care about gracefully catching this right now
found.value = `${data.clientFn}, ${data.clientLn}`; // adds 'undefined, undefined'
clientId = data.clientId;
// google.script.host.close();
}
setClient.addEventListener("click", ()=>{
google.script.run.setClient(fn, ln, clientId);
})

</script>
</body>

可以看到,我已经添加了一个"alert"在成功处理程序只是为了跟踪data,即使我已经测试了代码和无限,它总是返回一个完全可行的对象,这似乎总是显示null

这也是我的。gs函数:

function searchDb(fn, ln) {
const ws = SpreadsheetApp.openById("the ID of the SS, obviously").getSheets()[0];
const dataRange = ws.getRange(2, 1, ws.getLastRow() - 1, 3).getValues();
const lnRange = dataRange.map(r => { return r[2] });
const fnRange = dataRange.map(r => { return r[1] });
const idRange = dataRange.map(r => { return r[0] });
for (let a = 0; a < lnRange.length; a++) {
const clientLn = lnRange[a];
const clientFn = fnRange[a];
const clientId = idRange[a];
if (clientLn.includes(ln) && clientFn === fn) {
const data = {
clientFn: clientFn,
clientLn: clientLn,
clientId: clientId
}
return data;
}
}
return;
}

这确实是一个非常简单的程序,当我测试它时,它确实返回具有适当数据的data对象,因此不确定这里发生了什么。

我很确定我错过了一些很明显的东西,但我已经到了头撞墙的地步了…

任何帮助将不胜感激!

编辑

我已经编辑了代码,因为我最初发布的内容不一致-并且没有从尝试其他方法完全恢复的结果-。但是,问题仍然存在。

这不是一个数组:

const data = {'clientFn': clientFn,'clientLn': clientLn,'clientId': clientId}

但是你把它当作一个数组:

found.value = `${data[1]}, ${data[0]}`; 
clientId = data[2];

尝试引用它们的属性'clientFn','clientLn','clientId'

found.value = `${data['clientLn']}, ${data['clientFn']}`; 
clientId = data['clientId'];

你也可以参考这个链接来确保它们都是合法的类型。

事实证明问题与withSuccessHandler无关,正如我所料,而是与数据如何传递到searchDb有关,因为varsfnln在单击搜索按钮时就脚本而言没有改变值。解决方案很简单,只需在之后声明这些变量搜索按钮已被点击:

<script>
const search = document.getElementById('search');
const fullName = document.getElementById('fullName');
const country = document.getElementById('country');
const email = document.getElementById('email');
const noClient = document.getElementById('noClient');
const useClient = document.getElementById('useClient');

let clientId; // The ID of the client in the database;
search.addEventListener("click", () => {
const fn = document.getElementById('fn').value;
const ln = document.getElementById('ln').value;
google.script.run.withSuccessHandler(addClient).searchDb(fn, ln);
});
// ...etc

最新更新