When UrlFetchApp
由自定义函数和脚本编辑器使用,我认为区别在于是否使用IPv6,而IPv4的地址每次运行都会改变。在这种情况下,脚本编辑器和自定义菜单的结果是相同的。我认为这可能是您问题的原因。但我不确定我的猜测是否正确。因此,在这个答案中,我想提出以下解决方法。
- Put the formula
=getMyArray()
to a cell using the script.
- 使用脚本从单元格中检索值。
- 清除看跌期权公式。
通过这个流程,我认为你的目标可以实现。
示例脚本如下。
示例脚本:
在此脚本中,作为测试,=getMyArray()
被放入活动工作表上的单元格“A1”,并从该单元格中检索该值。使用此功能时,请运行该功能main()
在脚本编辑器和自定义菜单中。这样,就可以将值检索到array
.
function getMyArray() {
var url = "https://api.binance.com/api/v3/ticker/price";
var params = {"method": "get", "muteHttpExceptions": true};
var response = UrlFetchApp.fetch(url, params);
return response.getContentText();
}
// Please run this function by the script editor and the custom menu.
function main() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1");
range.setFormula("=getMyArray()");
SpreadsheetApp.flush();
var value = range.getValue();
range.clearContent();
var array = JSON.parse(value);
console.log(array)
}
参考:
- Google 表格中的自定义函数
- 设置公式()
- flush()
Added:
响应值来自https://httpbin.org/get
如下。
用于测试此功能的示例脚本:
function sample() {
var url = "https://httpbin.org/get";
var res = UrlFetchApp.fetch(url);
console.log(res.getContentText())
return res.getContentText();
}
Result:
Pattern 1. Script is run with the script editor.
{
"args": {},
"headers": {
"Accept-Encoding": "gzip,deflate,br",
"Host": "httpbin.org",
"User-Agent": "Mozilla/5.0 (compatible; Google-Apps-Script; beanserver; +https://script.google.com; id: ###)",
"X-Amzn-Trace-Id": "Root=###"
},
"origin": "### IPV6 ###, ### IPV4 ###", // or "### IPV4 ###, ### IPV4 ###"
"url": "https://httpbin.org/get"
}
- 当您使用 IPV6 时,
origin
is "### IPV6 ###, ### IPV4 ###"
。但是当你使用IPV4时,origin
is "### IPV4 ###, ### IPV4 ###"
.
- 在这种情况下,无法从中检索到正确的值
https://api.binance.com/api/v3/ticker/price
.
Pattern 2. Script is run with the custom function.
在这种情况下,=sample()
被放入单元格并检索该值。
{
"args": {},
"headers": {
"Accept-Encoding": "gzip,deflate,br",
"Host": "httpbin.org",
"User-Agent": "Mozilla/5.0 (compatible; Google-Apps-Script; beanserver; +https://script.google.com; id: ###)",
"X-Amzn-Trace-Id": "Root=###"
},
"origin": "### IPV4 ###",
"url": "https://httpbin.org/get"
}
- 在这种情况下,可以从以下位置检索正确的值
https://api.binance.com/api/v3/ticker/price
.
Pattern 3. Script is run with the OnEdit event trigger.
When UrlFetchApp
与自定义功能配合使用,无需授权。但当UrlFetchApp
与OnEdit事件触发器一起使用,授权需要安装可安装的触发器。我以为这个授权可能会出现这个问题。所以我就这样比较了。
When UrlFetchApp
与可安装的 OnEdit 事件触发器一起使用,将检索以下结果。
{
"args": {},
"headers": {
"Accept-Encoding": "gzip,deflate,br",
"Host": "httpbin.org",
"User-Agent": "Mozilla/5.0 (compatible; Google-Apps-Script; beanserver; +https://script.google.com; id: ###)",
"X-Amzn-Trace-Id": "Root=###"
},
"origin": "### IPV4 ###",
"url": "https://httpbin.org/get"
}
- 这个结果和上面的模式2是一样的。
- 在这种情况下,可以从以下位置检索正确的值
https://api.binance.com/api/v3/ticker/price
.
Result:
- 标头包括
User-Agent
所有模式都相同。
- 从模式2和模式3来看,与Google方的授权无关。
- When WHOIS with IPV4 is retrieved, the same result is returned.
- When
origin
is "### IPV4 ###, ### IPV4 ###"
,第二个IPV4是Google的IP地址。
从上面的结果来看,所有模式的不同之处在于是否origin
是1或2。