如何快速上手Google Spreadsheet:面向新手的5个实用示例
如何快速上手Google Spreadsheet面向新手的5个实用示例【免费下载链接】node-google-spreadsheetGoogle Sheets API wrapper for Javascript / Typescript项目地址: https://gitcode.com/gh_mirrors/no/node-google-spreadsheetGoogle Spreadsheet是一款强大的在线电子表格工具而node-google-spreadsheet作为JavaScript/TypeScript的Google Sheets API包装器让开发者能够轻松地通过代码与Google表格进行交互。本文将为新手提供5个实用示例帮助你快速掌握使用node-google-spreadsheet操作Google表格的基本技能。1. 环境准备与安装要开始使用node-google-spreadsheet首先需要确保你的开发环境中已经安装了Node.js。然后通过npm或yarn安装该库npm install google-spreadsheet # 或者 yarn add google-spreadsheet如果你需要从源码构建可以克隆仓库git clone https://gitcode.com/gh_mirrors/no/node-google-spreadsheet2. 身份验证获取访问权限在使用node-google-spreadsheet之前你需要进行身份验证以获取访问Google表格的权限。该库依赖google-auth-library模块来处理大多数身份验证需求。常见的身份验证方式有以下几种服务账号认证创建服务账号并下载JSON密钥文件后可以在项目中使用该文件进行认证。如果你使用环境变量进行配置只需关注JSON文件中的client_email和private_key。例如const { GoogleSpreadsheet } require(google-spreadsheet); const creds require(./service-account-creds.json); async function accessSpreadsheet() { const doc new GoogleSpreadsheet(YOUR-DOC-ID); await doc.useServiceAccountAuth(creds); await doc.loadInfo(); // 加载文档信息 console.log(文档标题: ${doc.title}); }或者更推荐从环境变量加载信息例如await doc.useServiceAccountAuth({ client_email: process.env.GOOGLE_CLIENT_EMAIL, private_key: process.env.GOOGLE_PRIVATE_KEY, });API密钥认证API密钥只能提供对公开文档的只读访问权限。使用方法如下const doc new GoogleSpreadsheet(YOUR-DOC-ID, { apiKey: process.env.GOOGLE_API_KEY });3. 读取表格数据成功认证后你可以轻松读取表格中的数据。以下是一个简单的示例展示如何加载文档信息并读取工作表数据async function readSpreadsheet() { const doc new GoogleSpreadsheet(YOUR-DOC-ID); await doc.useServiceAccountAuth(creds); await doc.loadInfo(); // 加载文档信息 const sheet doc.sheetsByIndex[0]; // 获取第一个工作表 console.log(工作表标题: ${sheet.title}); console.log(行数: ${sheet.rowCount}); const rows await sheet.getRows(); // 获取所有行数据 rows.forEach(row { console.log(row); }); }4. 写入和更新数据node-google-spreadsheet不仅可以读取数据还可以向表格中写入和更新数据。以下是一个添加新行和更新现有行的示例async function writeAndUpdateData() { const doc new GoogleSpreadsheet(YOUR-DOC-ID); await doc.useServiceAccountAuth(creds); await doc.loadInfo(); const sheet doc.sheetsByIndex[0]; // 添加新行 const newRow await sheet.addRow({ name: John Doe, email: johnexample.com, age: 30 }); console.log(添加新行: ${newRow.rowNumber}); // 更新现有行 const row await sheet.getRow(2); // 获取第二行 row.email theoexample.com; Object.assign(row, { first_name: Theo, email: theoexample.com }); await row.save(); }5. 导出表格数据你还可以将表格数据导出为不同的格式如CSV、XLSX等。以下是一个将工作表导出为CSV文件的示例const fs require(fs); async function exportData() { const doc new GoogleSpreadsheet(YOUR-DOC-ID); await doc.useServiceAccountAuth(creds); await doc.loadInfo(); const sheet doc.sheetsByIndex[0]; const csvBuffer await sheet.downloadAsCSV(); await fs.writeFile(./exported-data.csv, csvBuffer); console.log(数据已导出为CSV文件); }对于整个文档的导出可以使用类似的方法async function exportDocument() { const doc new GoogleSpreadsheet(YOUR-DOC-ID); await doc.useServiceAccountAuth(creds); await doc.loadInfo(); const xlsxBuffer await doc.downloadAsXLSX(); await fs.writeFile(./my-export.xlsx, Buffer.from(xlsxBuffer)); console.log(文档已导出为XLSX文件); }总结通过以上5个实用示例你已经了解了node-google-spreadsheet的基本用法包括环境准备、身份验证、读取数据、写入更新数据以及导出数据。这个强大的库为开发者提供了便捷的方式来与Google表格进行交互无论是构建数据管理工具还是自动化报表生成都能发挥重要作用。开始尝试使用node-google-spreadsheet探索更多可能吧【免费下载链接】node-google-spreadsheetGoogle Sheets API wrapper for Javascript / Typescript项目地址: https://gitcode.com/gh_mirrors/no/node-google-spreadsheet创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考