@flink-app/google-sheets-plugin (英语)
使用服务账户认证从 Flink 应用读取并写入 Google 工作表 .
先决条件
- 在 谷歌云控台 ——IAM与管理 ——服务账户
- 启用 谷歌工作表 API 用于您的项目
- 与服务账户电子邮件共享电子表格( 编辑器访问)
注册插件
import { googleSheetsPlugin, GoogleSheetsPluginCtx } from "@flink-app/google-sheets-plugin";
interface AppCtx extends FlinkContext<GoogleSheetsPluginCtx> {
repos: {};
}
new FlinkApp<AppCtx>({
plugins: [
googleSheetsPlugin({
spreadsheetId: process.env.GOOGLE_SPREADSHEET_ID!,
credentials: {
clientEmail: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL!,
privateKey: process.env.GOOGLE_PRIVATE_KEY!,
},
}),
],
});
私人密钥接受原始的 PEM 字符串( 有 \n)或基础64-编码服务账户JSON blob.
使用量
通过 ctx.plugins.googleSheets启用 .sheet("Title") 或者说 .sheetByIndex(n) 将操作范围改为特定工作表.
// Read all rows
const rows = await ctx.plugins.googleSheets.sheet("Tasks").getRows();
// [{ rowIndex: 0, data: { title: "Fix bug", status: "pending" } }, ...]
// Append a row
const row = await ctx.plugins.googleSheets.sheet("Tasks").appendRow({
title: "New task",
status: "pending",
});
// Update a row
await ctx.plugins.googleSheets.sheet("Tasks").updateRow(row.rowIndex, {
status: "completed",
});
// Delete a row
await ctx.plugins.googleSheets.sheet("Tasks").deleteRow(row.rowIndex);
// Spreadsheet metadata
const info = await ctx.plugins.googleSheets.getInfo();
// { title, spreadsheetId, sheetCount }
行为普通对象 : { rowIndex: number, data: Record<string, string> } 其中键为列头.
动态全权证书
启动时从数据库装入证书, 而不是 env vars :
googleSheetsPlugin({
loadCredentials: async (ctx) => {
const config = await ctx.repos.configRepo.getOne({ key: "google" });
return {
credentials: {
clientEmail: config.serviceAccountEmail,
privateKey: config.privateKey,
},
spreadsheetId: config.spreadsheetId,
};
},
});
AI 工具
通用软件 GoogleSheetsTool 包含用于Flink代理。 它支持 getRows, (中文(简体) ), appendRow, (中文(简体) ), updateRow, (中文(简体) ), deleteRow,以及 getInfo 操作.
import GoogleSheetsTool, { Tool } from "@flink-app/google-sheets-plugin/tools/GoogleSheetsTool";
class MyAgent extends FlinkAgent<AppCtx> {
tools = [{ ...Tool, handler: GoogleSheetsTool }];
}