本文介绍了Gulp:调用异步函数,该函数在转换函数中提供自己的回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Gulp中创建一个在pipe()调用中使用的函数,该函数可以将xlsx文件转换为json。

I want to create a function for use in a pipe() call in Gulp that'll enable conversion of xlsx files to json.

gulp 3的NPM软件包'excel-as-json',但是Gulp 4迫使我真正了解它在做什么;-)

I had this working with NPM package 'excel-as-json' for gulp 3, however Gulp 4 has forced me to actually understand what it is doing ;-)

六个小时后,由于缺乏js / async / streaming知识而使其无法工作,这激起了我的好奇心。

Six hours in, and my incapacity of getting this to work due to a lack of js/async/streaming knowledge is punching my curiosity.

代码如下:

paths = {excel_sourcefiles: "./sourcefiles/*.xls*", excel_targetdir_local_csvjson: "./targetfiles_local/*.*"}


var replaceExt = require('replace-ext');
var PluginError = require('plugin-error')
var gulpFunction = require('gulp-function').gulpFunction // default ES6 export
var through = require("through2")
var convertExcel = require('excel-as-json').processFile;
var changed = require('gulp-changed');
var assign = Object.assign || require('object.assign');
var notify = require('gulp-notify');
var gulpIgnore = require('gulp-ignore');
var rename = require('gulp-rename');

gulp.task('excel-to-jsoncsv', function() {
  return gulp.src(paths.excel_sourcefiles)
    // .pipe(debug())
    .pipe(gulpIgnore.exclude("*\~*")) // Ignore temporary files  by Excel while xlsx is  open
    .pipe(gulpIgnore.exclude("*\$*")) // Ignore temporary files  by Excel while xlsx is  open
    .pipe(plumber({errorHandler: notify.onError('gulp-excel-to-jsoncsv error: <%= error.message %>')}))
    .pipe(changed(paths.excel_targetdir_local_glob, { extension: '.csv' }))
    .pipe(GulpConvertExcelToJson()) // This is where the magic should happen
    .pipe(rename({ extname: '.csv' })) // Saving as .csv for SharePoint (does not allow .json files to be saved)
    .pipe(gulp.dest(paths.excel_targetdir_local))
});

function GulpConvertExcelToJson() {
  return through.obj(function(chunk, enc, callback) {
    var self = this
    if (chunk.isNull() || chunk.isDirectory()) {
      callback() // Do not process directories
      // return
    }

    if (chunk.isStream()) {
      callback() // Do not process streams
      // return
    }

    if (chunk.isBuffer()) {
      convertExcel(chunk.path, null, null, // Converts file found at `chunk.path` and returns (err, `data`) its callback.
        function(err, data) {
          if (err) {
            callback(new PluginError("Excel-as-json", err))
          }
          chunk.contents = new Buffer(JSON.stringify(data))
          self.push(chunk)
          callback()
          // return
        })
    } else {
      callback()
    }
  })
}

我(现在)知道还有其他excel> json gulp模块可以使我无需编写自己的模块即可解决此问题,但我想了解在这里应该做些什么。

I (now) know there are other excel > json gulp modules that could allow me to solve this problem without writing my own module, yet I'd like to understand what I should do different here.

返回的错误是您是否忘记表示异步完成?,我试着不这样做。但是,可能我尝试使用 var self = this 修复错误 this.push不是函数不是我应该做的事情。

The error returned is 'Did you forget to signal Async completion?', which I tried to not do. However, probably my attempt to fix the error 'this.push is not a function' with a var self = this is not what I was supposed to do.

看着类似gulp-zip的示例,使我认识到了陌生的记号,这使我无法自动摆脱困境。

Looking at examples like gulp-zip introduced me to unfamiliar notations, making me unable to autodidact my way out of this.

总结:


  • 如何通过through.obj函数调用来调用异步函数,其中块的内容将通过(不受我的控制)异步功能仅向我提供回调(错误,数据)?

推荐答案

此问题来自 excel 库。 。

This problem is coming deep from the excel library. The end event should be finish.

I执行以下操作,安装修改后的excel.js:

I did the following, to install the modified excel.js:

rm -rf node_modules/excel
git clone https://github.com/bdbosman/excel.js node_modules/excel
cd node_modules/excel && npm i && cd ../..

由于 excel-as -json 使用旧版本的 excel

您必须修改 excel-as-json 模块,以使用 excel@1.0.0 (在合并请求合并后)或手动使用在node_modules中编辑文件。我将在这里描述第二种方法。

Either you have to modify the excel-as-json module, to use excel@1.0.0 (after the Pull Request is merged) or manually edit the file in node_modules. I will describe the second way here.

编辑 excel 文件,安装模块后。

Edit the excel file after installing the modules.

我使用过:

sed -i "s/'end'/'finish'/g" node_modules/excel/excelParser.js

然后我运行:

$ gulp excel-to-jsoncsv
[19:48:21] Using gulpfile ~/so-question-gulp-async-function-call-xlsx/gulpfile.js
[19:48:21] Starting 'excel-to-jsoncsv'...
[19:48:21] Finished 'excel-to-jsoncsv' after 126 ms

工作。

这篇关于Gulp:调用异步函数,该函数在转换函数中提供自己的回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 11:45
查看更多