如何呈现数组数据,该数据已从Google Sheet API导入到数组变量中

时间:2019-06-03 02:35:35

标签: javascript node.js express

我有一个Array变量,该变量从Google Sheet API获取数据。我想在某些特定页面中呈现数据并显示该数组的数据。

//app.js Code

var express = require('express');
var app = express();
app.set('view engine', 'ejs');
var routes = require('./routes');
var path = require('path');
app.use(express.static(path.join(__dirname,'public')));

//Google SHeet API

app.get('/excel',routes.excel);

//index.js Code

//Google Sheet API
exports.excel=(req,res)=>{
  const {google} = require ('googleapis');
const keys = require('../keys.json');

const client = new google.auth.JWT(
        keys.client_email, null,keys.private_key,
        ['https://www.googleapis.com/auth/spreadsheets']
);

client.authorize(function(err, tokens){
    if(err){
      console.log(err);
      return;
    }else{
      console.log('Connected');
      gsrun(client);
    }

   });
   async function gsrun(cl){
  const gsapi = google.sheets({
    version:'v4',auth:cl});
    const opt = {
      spreadsheetId:'1cPNCt3mTyYN3Mor7S6CPxzCJFfHwVKF3jgzF31fp0jM',
      range: 'Data!A2:B5'
    };
    var data = await gsapi.spreadsheets.values.get(opt);
    var dataArray =data.data.values;
    console.log(dataArray);
     }
    res.render('excel',{
    title:"My Google Sheet API",
    dataArray:dataArray
      });
    }


    // final excel.ejs Page


   <li><%= dataArray.lenght %></li>

在index.js页面dataArrey中出现错误,未定义。我想通过渲染dataArray将数据从我的Google表格显示到excel.ejs页面。但是,我得到了控制台日志数据。

0 个答案:

没有答案