简体   繁体   English

使用“convert-excel-to-json”在excel中提取值并赋值给变量?

[英]Using “convert-excel-to-json” to extract a value in excel and assign to variable?

Im learning how to use installed npm module "convert-excel-to-json" to extract one cell value in excel and assign to a variable in node. 我学习如何使用已安装的npm模块“convert-excel-to-json”来提取excel中的一个单元格值并分配给节点中的变量。 My excel workbook has 8 values (A1 to A8) & called sheet1. 我的excel工作簿有8个值(A1到A8)并称为sheet1。 Here is the code: 这是代码:

'use strict'
const fs = require('fs')
const excelToJson = require('convert-excel-to-json')

const result = excelToJson({ source: fs.readFileSync('excel-action-buffer.xlsx') })
console.log(result)

The output is: 输出是:

{ Sheet1:
   [ { A: 'ENDPOINT',
       B: 'OPTIONS',
       C: 'METHOD',
       D: 'SIZE',
       E: 'PRICE',
       F: 'SIDE',
       G: 'PRODUCT',
       H: 'FUNDS' } ] }

How would you assign one of these values to a variable? 您如何将其中一个值分配给变量? Iv read the documentation but the methods seem to be for more advanced filtering where I am just pulling a value from an array. 我阅读了文档,但方法似乎是更高级的过滤,我只是从数组中提取一个值。 So ie 'console.log(result.price) returns undefined?? 那么'console.log(result.price)返回undefined ??

PRICE is the value, and the keys needed to access it are the sheet name, row index, and column name : PRICE是值,访问它的关键是工作表名称,行索引和列名称:

 var result = { Sheet1: [ { A: 'ENDPOINT', B: 'OPTIONS', C: 'METHOD', D: 'SIZE', E: 'PRICE', F: 'SIDE', G: 'PRODUCT', H: 'FUNDS' } ] }; console.log( result.Sheet1[0].E ); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM