我正在使用EXCELJS从Excel文件读取数据。创建了一个函数 allinExcel ,该函数返回promise中的列值。因为我需要2列,所以已经链接了类似的promise函数- allinExcel 。在这种情况下,有时候,我会得到预期的结果,而有时候却没有,这是模棱两可的。 ProExcel文件由2列组成。以下代码的预期结果是2个数组,其中包含每个列的内容。任何帮助都非常有用。
预期的结果是[ <1 empty item>, 'GEL', 'BEL', 'HEL', 'SEL' ] [ <1 empty item>, 55, 555, 66, 666 ]
但是,如果我使用一个promise获得列值,那么每次都会得到结果而没有任何歧义。
describe("Excel Read ",function(){
function allinExcel(colNum){
var Excel = require('exceljs');
var workbook = new Excel.Workbook();
var excelFilePath = "ExcelData/ProExcel.xlsx"
return workbook.xlsx.readFile(excelFilePath).then(function() {
var worksheet=workbook.getWorksheet('Sheet1');
return worksheet.getColumn(colNum+1).values
},function(error){
console.log(error)
return fail
})
}
it("Excel Operation",function(){
allinExcel(0).then(function(col0){
allinExcel(1).then(function(col1){
console.log(col1,col0)
})
})
})
以下是conf.js文件
// conf.js
var Jasmine2HtmlReporter = require('protractor-jasmine2-html-reporter');
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
getPageTimeout:20000, // browser.get timeout
allScriptsTimeout: 360000, // Time to load the DOM
jasmineNodeOpts: {defaultTimeoutInterval: 50000}, //For individual it
framework: 'jasmine',
capabilities: {
'browserName': 'chrome',
chromeOptions: {
args: [
'--start-maximized','disable-infobars'//,'--headless'
]
}
},
onPrepare: function() {
global.EC = protractor.ExpectedConditions,
jasmine.getEnv().addReporter(
new Jasmine2HtmlReporter({
savePath: 'target/reports',
screenshotsFolder: 'images',
takeScreenshots: true,
takeScreenshotsOnlyOnFailures: true,
fixedScreenshotName: false,
fileName: 'currentRun',
cleanDestination: true
})
);
},
jasmineNodeOpts: {
showColors: true, // Use colors in the command line report.
},
specs: ['specs/WebTables.js'],
};
答案 0 :(得分:0)
请尝试在javascript文件中使用函数和代码,而不使用量角器,以查看您的代码能否正常工作。
test.js in the temp folder
的文件,然后将以下代码复制到该文件中。ProExcel.xlsx
复制到临时文件夹中在cmd窗口中运行node test.js
// test.js
function allinExcel(colNum) {
var Excel = require('exceljs');
var workbook = new Excel.Workbook();
var excelFilePath = "ProExcel.xlsx"
return workbook.xlsx.readFile(excelFilePath).then(function () {
var worksheet = workbook.getWorksheet('Sheet1');
return worksheet.getColumn(colNum + 1).values
}, function (error) {
console.log(error)
return fail
})
}
[1, 2, 3, 4, 5, 6, 7].forEach(function (it, index) {
allinExcel(0).then(function (col0) {
allinExcel(1).then(function (col1) {
console.log('\n#### ' + index)
console.log(col0, col1)
})
})
})
如果在每个循环中都无法获得相同的结果,请更改您的nodejs版本(我使用v10.9.0)。
如果在每个循环中都能获得相同的结果,则问题出在量角器一侧。
为return
的每个嵌套函数添加then()
,再次运行。
it("Excel Operation",function(){
allinExcel(0).then(function(col0){
return allinExcel(1).then(function(col1){
return console.log(col1,col0)
})
})
})