在Node.js Google电子表格上运行async.series

时间:2019-07-15 05:41:19

标签: node.js async.js

我想使用google-spreadsheet(nodejs)读取和更新Google表格上的数据,首先读取记录并使用该记录上的值。

async.series([
 function setAuth(step) {
    doc.useServiceAccountAuth(creds, step);
 },
   function getInfoAndWorksheets(step) {
    doc.getInfo(function(err, info) {
      console.log('Loaded doc: '+info.title+' by '+info.author.email);
      sheet = info.worksheets[0];
      console.log('sheet 1: '+sheet.title+' '+sheet.rowCount+'x'+sheet.colCount);
      step();
    });
  },
 function workingWithCells(step) {
    // Get all of the cell value from the spreadsheet.
    console.log("get cell value"); 
    doc.getCells(2, {
        'min-row': 1,
        'max-row': 1,
        'min-col':2,
        'max-col':2,
        'return-empty': true
    }, function(err, data) {         
         var cell = data[0];
        cb=cell.value;console.log("first:"+cb);    
    });
    step();
},


   // executes after one second, and blocks the thread
    function savedata(step) {
    // sleep(20000, function() {
        console.log("second:"+cb);
    NoRow = cb+1;
    if(cb !== null && cb !== '') {
        sheet.getCells(2, {
            'min-row': NoRow,
            'max-row': NoRow,
            'return-empty': true
        }, function(err, data) {
             var cell1 = itemToSave;
            sheet.bulkUpdateCells(cell1);
        console.log("Saved");

        });
    }
    else {
          // Get all of the rows from the spreadsheet.
        doc.addRow(1, itemToSave, function(err) {
        if(err) {
            console.log(err);
            }
        });

    };
    step();
//});
},



], function(err){
    if( err ) {
      console.log('Error: '+err);
    }
});

我希望输出

  

加载的文档:xxxx
  表格1:xxxxxx
  获取单元格值
  首先:xxxx
  秒:xxxx

但是出错:

  

加载的文档:xxxx
  表格1:xxxx
  获取单元格值
  第二:未定义
  第一:xxx
  events.js:180
  投掷者//未处理的“错误”事件

1 个答案:

答案 0 :(得分:0)

您正在step进行异步工作完成前的操作。

async.series([
  function setAuth(step) {
    doc.useServiceAccountAuth(creds, step);
  },
  function getInfoAndWorksheets(step) {
    doc.getInfo(function (err, info) {
      console.log('Loaded doc: ' + info.title + ' by ' + info.author.email);
      sheet = info.worksheets[0];
      console.log('sheet 1: ' + sheet.title + ' ' + sheet.rowCount + 'x' + sheet.colCount);
      step();// here
    });
  },
  function workingWithCells(step) {
    // Get all of the cell value from the spreadsheet.
    console.log("get cell value");
    doc.getCells(2, {
      'min-row': 1,
      'max-row': 1,
      'min-col': 2,
      'max-col': 2,
      'return-empty': true
    }, function (err, data) {
      var cell = data[0];
      cb = cell.value;
      console.log("first:" + cb);
      step(); //<---here
    });
  },
  // executes after one second, and blocks the thread
  function savedata(step) {
    // sleep(20000, function() {
    console.log("second:" + cb);
    NoRow = cb + 1;
    if (cb !== null && cb !== '') {
      sheet.getCells(2, {
        'min-row': NoRow,
        'max-row': NoRow,
        'return-empty': true
      }, function (err, data) {
        var cell1 = itemToSave;
        sheet.bulkUpdateCells(cell1);
        console.log("Saved");
        step();
      });
    }
    else {
      // Get all of the rows from the spreadsheet.
      doc.addRow(1, itemToSave, function (err) {
        if (err) {
          console.log(err);
        }
        step();// here
      });
    };
    //});
  },
], function (err) {
  if (err) {
    console.log('Error: ' + err);
  }
});

此外,请避免使用全局变量。