google.script.run.withSuccessHandler()返回未定义 [英] google.script.run.withSuccessHandler() returns Undefined
本文介绍了google.script.run.withSuccessHandler()返回未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用下面提供的代码在单独的GS文件中创建了一个数组.我尝试在我的HTML文件中调用它.我的目标是将数组的内容与参数email
进行比较.但是,google.script.run.withSuccessHandler()返回的值是undefined
I created an array in a separate GS file using the code provided below. I tried calling it in my HTML file. My goal is to compare the contents the array to the parameter email
. However, the value returned by google.script.run.withSuccessHandler() is undefined
//in GS
function mailGetter()
{
//open sheet
var sheet = SpreadsheetApp.openByUrl("https://sheet.url").getSheetByName("Email Sheet").activate();
//get size of given row range
var row_data_email = sheet.getRange("C2:C").getValues();
var emailArray = row_data_email.join().split(',').filter(Boolean);
Logger.log(emailArray);
return emailArray;
}
//in HTML
function checkEmail(email)
{
var reg1 = /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/;
var arraySize = google.script.run.withSuccessHandler(misc).sizeGetter();
console.log(arraySize);
var emailArray = new Array(arraySize);
emailArray = google.script.run.withSuccessHandler(misc).mailGetter();
console.log(emailArray);
if (reg1.test(email) == false)
{
emails.style.border = "1px solid red";
document.getElementById('submitBtn').disabled = true;
}
else if (reg1.test(email) == true)
{
emails.style.border = "1px solid green";
document.getElementById('submitBtn').disabled = false;
}
for (var row = 0; row < arraySize; row++)
{
if (emailArray[row][0] == email)
{
emails.style.border = "1px solid green";
document.getElementById('submitBtn').disabled = false;
break;
}
else if (emailArray[row][0] != email)
{
emails.style.border = "1px solid red";
document.getElementById('submitBtn').disabled = true;
}
}
}
function misc()
{
console.log("Pass");
}
推荐答案
问题:
- 使用异步函数的(
google.script.run
)返回值,该值始终为undefined
. - Using a asynchronous function's(
google.script.run
) return value, which will always beundefined
. - 使用另一个答案中提到的successHandler,或者我们可以在async/await中使用promises.
Issue:
/*Create a promise around old callback api*/
const p = func =>
new Promise(resolve=>
google.script.run.withSuccessHandler(resolve)[func]()
);
async function checkEmail(email) //modified
{
var arraySize = await p('sizeGetter');//Wait to resolve
console.log(arraySize);
//var emailArray = new Array(arraySize);
var emailArray = await p('mailGetter');//Wait to resolve
console.log(emailArray);
//....
}
注意:
- 最好减少对服务器的呼叫次数.如果您可以将两个
Getter
都合并到一个服务器功能中,那就更好了. - 以上是显示如何使用
async
/await
的代码段.但是,如果您如上所述等待服务器的每个响应,则前端/UI会很慢.仅在绝对必要时等待.到服务器的调用应该是非阻塞/异步的. - It's better to reduce the number of calls to the server. If you can combine both
Getter
s to a single server function, it'll be better. - The above is a snippet showing how to use
async
/await
. But if you wait for each response from the server as shown above, your front end/UI will be slow. Wait only if absolutely necessary. Calls to server should be non-blocking/asynchronous. - Promises
- async
- await
Note:
这篇关于google.script.run.withSuccessHandler()返回未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文