如何通过单击 html 中的按钮 运行 节点 js 文件(进行 api 调用并将数据放入 sheet)

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 脚本。 node js文件从excel sheet中拉取数据并放入特定的sheet.

如果我在控制台中 运行 节点 js 文件可以完美运行。但是如何将此文件与 html 中的按钮连接起来?仅仅调用函数是不可能的,因为node js文件在服务器上运行ning。

这是我想通过点击按钮执行的功能

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 端点(例如通过 运行 围绕 express 模块构建的服务器或使用 AWS Lambda 等云服务)然后使用客户端 JS 来制作请求(例如使用 Fetch API)。

你必须为此调用公开的 api。 您的 nodeJs 脚本必须监听端点。

从 JavaScript 在您的 html 您必须使用 fetch 呼叫您的 api。