如何通过点击html中的按钮来运行node-js文件(该文件进行api调用并将数据放入工作表)



我试图在这里得到一些关于堆栈溢出的答案,但我并不真正理解。

我想通过点击html中的一个按钮来执行我的node-js脚本。node-js文件从excel表中提取数据,并将其放入特定的表中。

如果我在控制台中运行node-js文件,它就可以完美地工作。但是我如何将这个文件与html中的按钮连接起来呢?仅仅调用函数是不可能的,因为node-js文件正在服务器上运行。

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

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。您的nodeJ脚本必须侦听端点。

从html中的JavaScript,您必须使用fetch调用您的api。

最新更新