簡體   English   中英

如何通過單擊 html 中的按鈕來運行節點 js 文件(進行 api 調用並將數據放入工作表中)

[英]How to run a node js file (that makes a api call and puts data into a sheet) by clicking on a button in html

我試圖在這里得到一些關於堆棧溢出的答案,但我不太明白。

我想通過單擊 html 中的按鈕來執行我的節點 js 腳本。 節點 js 文件從 excel 工作表中提取數據並將其放入特定工作表中。

如果我在控制台中運行節點 js 文件,它可以完美運行。 但是如何將此文件與 html 中的按鈕連接? 不能只調用 function,因為 node js 文件是在服務器上運行的。

這是我想通過單擊按鈕執行的 function

function importData(){
    //import the smartsheet
    ss.sheets.importXlsxSheet(options)
    .then(function(result) {
        console.log("Created sheet '" + result.result.id + "' from excel file");
        
        // Load entire sheet
        ss.sheets.getSheet({ id: result.result.id })
            .then(function(sourceSheet) {
                console.log("Loaded: " + sourceSheet.rows.length + " rows from sheet '" + sourceSheet.name + "'");
                console.log("Done");

                    //copy every row from sourcesheet in a array 
                    const sourceSheetRows = sourceSheet.rows;
                    writeRowsinArray(result, sourceSheetRows);
            })
            .catch(function(error) {
                console.log(error);
            });
    })
    .catch(function(error) {
        console.log(error);
    });
}

從瀏覽器觸發服務器上某些東西的標准方法是向它發出 HTTP 請求。

因此,設置一個 HTTP 端點,它將觸發 function(例如,通過運行圍繞 express 模塊構建的服務器或使用 AWS Lambda 等雲服務),然后使用客戶端 JS 發出請求(例如,使用 Fetch API)。

為此,您必須調用暴露的 api。 您的 nodeJs 腳本必須在端點上偵聽。

從 html 中的 JavaScript 開始,您必須使用fetch調用 api 。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM