运行 async.series 在 Nodejs google-电子表格上

Run async.series on Nodejs google-spreadsheet

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

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);
    }
});

我期望输出

Loaded doc: xxxx
sheet 1: xxxxxx
get cell value
first: xxxx
second:xxxx

但是错误:

Loaded doc: xxxx
sheet 1:xxxx
get cell value
second:undefined
first:xxx
events.js:180
throw er; //Unhandled 'error' event

您正在 stepping 异步工作完成之前。

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);
  }
});

另外,不要使用全局变量。