gpt4 book ai didi

javascript - Google App Script Cache 数据类型以及如何转换为字符串

转载 作者:行者123 更新时间:2023-11-30 14:30:09 26 4
gpt4 key购买 nike

function receiveText(form){//code that text is received and saved
var nameBox = form.text;
//Logger.log(nameBox);
var cache = CacheService.getUserCache();
cache.put("text",nameBox);
}
function items() {//compare and put out the text
var cache = CacheService.getUserCache();
//cache.put("text","hello");
var exmp =cache.get("text");
var x=String(exmp);
if(x=="hello")
return( "Password correct" );
else return x;

}

<HTML>
<!DOCTYPE html>
<html>
<head>
<title>Test Page</title>
<p id="demo"></p>
<body>
<form>
<input type="text" value=" " name="text" />
<input type="button" onClick="formSubmit()" value="Submit" />
</form>
</body>
<script type="text/javascript">
function formSubmit() {
google.script.run.receiveText(document.forms[0]);
google.script.run.withSuccessHandler(onSuccessed).items();
}
function onSuccessed(items) {
document.getElementById('demo').innerHTML = items;
}
</script>
</html>

我正在用谷歌应用程序脚本编写!我永远无法击中项目中的 (x=="hello") block 任何人都可以帮助我,即使 x 的值将返回 hello 但我不知道是什么类型。
基本上,它是一个文本框,当单击按钮时,我会将信息存储到缓存中。然后,它将运行下一个从缓存中获取并处理信息的函数。这很奇怪,因为我可以强制我的缓存为 hello 并且 x 等于 hello 并将返回正确的密码并且它也适用于数字但不适用于字符串?

最佳答案

在您的例子中,检索到的值的类型是字符串。

这个答案怎么样?它假设“你好”被输入到文本框。在本例中,获取receiveText(form)的长度form.text时,长度为6。“hello”的长度为5,每个字符编码为[32,104,101,108,108,111]。即,最上面的字母是一个空格。由此,if(x=="hello") 始终为假。为了使用您的脚本消除此问题,请按如下方式修改 receiveText(form)

发件人:

var nameBox = form.text;

收件人:

var nameBox = form.text.trim();

注意事项:

  • 我认为针对您的情况有多种解决方案,因此请将此视为其中之一。
  • 由于 google.script.run 是异步的,当你从 CacheService 中获取值后,将值放入,下面的脚本可能会更好。关于这一点,请根据您的情况选择。
样本
google.script.run.withSuccessHandler(function() {
google.script.run.withSuccessHandler(onSuccessed).items();
}).receiveText(document.forms[0]);

引用:

关于javascript - Google App Script Cache 数据类型以及如何转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51330177/

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