![](/img/trans.png)
[英]How can I store the initial text/event-stream response from a get request in JavaScript when the server never stops loading?
[英]Is there a way to get a single response from a text/event-stream without using event listeners?
我正在 Google Sheets 中編寫一個腳本來從 API 中檢索一個值。 API 每 10 秒提供一次text/event-stream
響應。 有沒有一種方法可以在不使用異步函數或事件偵聽器的情況下檢索單個響應? 我在 JavaScript 方面不是很擅長,但是因為我在 Google 表格中工作,所以異步函數和事件偵聽器似乎無法正常工作。 從我到目前為止所學到的,處理text/event-stream
響應的唯一方法是使用EventSource
但我無法使其與 Google Sheets 一起使用。
不過,我的目標只是從端點檢索一個響應,因此我可以在 Google 表格中實現這一目標的任何方式都很棒。 這是端點,以防萬一:
因為我無法在 Google Sheets 中使用EventStream
,所以我嘗試使用在這里找到的 polyfil: https : //github.com/amvtek/EventSource/blob/master/dist/eventsource.js
然后運行它:
function getRplantTotal() {
var source = new EventSource('https://pool.rplant.xyz/api2/poolminer2x/raptoreum/RThRfoQJg8qsoStLk7QdThQGmpbFUCtvnk/UlRoUmZvUUpnOHFzb1N0TGs3UWRUaFFHbXBiRlVDdHZua3x4');
source.addEventListener("message", function(e) {
console.log(e.data);
});
}
但這只是輸出:
3:11:49 PM Notice Execution started
3:11:49 PM Notice Execution completed
我相信你的目標如下。
https://pool.rplant.xyz/api2/poolminer2x/raptoreum/RThRfoQJg8qsoStLk7QdThQGmpbFUCtvnk/UlRoUmZvUUpnOHFzb1N0TGs3UWRUaFFHbXBiRlVDdHZua3x4
的 URL 的 URL 中檢索第 1 個值,並希望使用 Google Appssheet 中的值來使用 Google Appssheet 檢索。 當我看到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.
來看, 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 Script 可以使用對話框和側邊欄從 Javascript 端檢索值。 從您的問題來看,當使用 Javascript 時,可以檢索該值。 我認為這可能可以使用。 在 Google Apps 腳本中反映此解決方法時,如下所示。
Code.gs
請將以下腳本復制粘貼到谷歌電子表格腳本編輯器的腳本文件中。
// 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.
}
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
函數中使用檢索到的值。在此解決方法中,需要通過瀏覽器執行腳本。 因為使用了Javascript。 所以,請注意這一點。
作為另一種解決方法,當您只能使用 Javascript 時,Sheets API 可以與 Javascript 一起使用。 Ref在這種情況下,也可以使用 Javascript 檢索值並將其放入電子表格。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.