我相信你的目标如下。
- 您希望使用 Google Apps 脚本从
https://pool.rplant.xyz/api2/poolminer2x/raptoreum/RThRfoQJg8qsoStLk7QdThQGmpbFUCtvnk/UlRoUmZvUUpnOHFzb1N0TGs3UWRUaFFHbXBiRlVDdHZua3x4 的 URL 中检索第一个值,并希望在 Google 电子表格中使用检索到的值。
问题和解决方法:
当我看到https://github.com/amvtek/EventSource/blob/master/dist/eventsource.js 时,似乎请求是使用XMLHttpRequest 运行的。在 Google Apps Script 中,使用了 UrlFetchApp,不能使用 XMLHttpRequest。我认为这可能是您当前问题的原因。但不幸的是,在当前阶段,这不能在 Google Apps Script 中使用text/event-stream 类型。当使用 UrlFetchApp 请求您的 URL 时,它看起来像无限循环。这是目前的情况。
所以,从My goal is just to retrieve one response from the endpoint though, so any way I can accomplish that in Google Sheets would be great. 和上述情况,我想提出一个解决方法。当您在 Google 电子表格上运行脚本时,如何使用 Javascript 从 URL 中检索值? Google Apps 脚本可以使用对话框和侧边栏从 Javascript 端检索值。根据您的问题,当使用 Javascript 时,可以检索该值。我认为这也许可以使用。当此解决方法反映在 Google Apps 脚本中时,如下所示。
示例脚本:
Google Apps 脚本端:Code.gs
请将以下脚本复制并粘贴到 Google 电子表格脚本编辑器的脚本文件中。
// Please run this function.
function main() {
SpreadsheetApp.getUi().showModalDialog(HtmlService.createHtmlOutputFromFile("index"), "sample");
}
function getValues(e) {
const obj = JSON.parse(e); // This is the 1st value from the URL of "https://pool.rplant.xyz/api2/poolminer2x/raptoreum/RThRfoQJg8qsoStLk7QdThQGmpbFUCtvnk/UlRoUmZvUUpnOHFzb1N0TGs3UWRUaFFHbXBiRlVDdHZua3x4"
console.log(obj)
// DriveApp.createFile("sample.txt", e); // When you use this, the retrieved value can be created as a text file.
}
Javascript 端:index.html
请将以下脚本复制并粘贴到 Google 电子表格脚本编辑器的 HTML 文件中。请将文件名设置为index.html。
Values are retrieving now. Please wait. After the values were retrieved, this dialog is automatically closed.
<script>
var source = new EventSource('https://pool.rplant.xyz/api2/poolminer2x/raptoreum/RThRfoQJg8qsoStLk7QdThQGmpbFUCtvnk/UlRoUmZvUUpnOHFzb1N0TGs3UWRUaFFHbXBiRlVDdHZua3x4');
source.addEventListener("message", function(e) {
source.close();
google.script.run.withSuccessHandler(google.script.host.close).getValues(e.data);
});
</script>
- 在此脚本中,请从脚本编辑器运行
main() 函数。这样,在电子表格上打开一个对话框,并使用 Javascript 从 URL 检索值,当检索到第一个值时,将值发送到 Google Apps 脚本端。所以你可以在getValues的函数中使用检索到的值。
注意:
参考资料: