【问题标题】:Triggers AWS Lambda Every Time a New Google Sheet Row is Added每次添加新的 Google 表格行时触发 AWS Lambda
【发布时间】:2022-04-04 09:23:45
【问题描述】:
我有一个谷歌表格,它从谷歌表单中收集答案,询问用户各种问题。每次用户提交表单时,都会在 google sheet 中添加一个新行。
我想编写 AWS Lambda 函数,以便每次出现新响应时,Lambda 函数都会运行并将新响应附加到 RDS MySql 数据库。
我使用 Python 编写脚本。我看到的大部分东西都是使用 node.js 我不知道从哪里开始。
谢谢
【问题讨论】:
标签:
python
amazon-web-services
google-sheets
aws-lambda
【解决方案2】:
根据您的 Google 表格的配置方式,您可以将 onEdit() 触发器与此 Google Apps Script library 结合使用,它完全支持 AWS 开发工具包。
您的最终函数可能如下所示:
// define your AWS configuration here (see dist/Config.js in the library example)
const AWS_CONFIG = {
accessKey: 'AK0ZXZD0KGNG4KG6REBP', // use your own AWS key
secretKey: 'EXrPgHC41HEW2YownLUnJLgh6bMsrmW1uva1ic24', // use your own AWS key
region: 'us-east-1',
};
function initConfig(config) {
AWS.config = new AWS.Config();
AWS.config.update({
region: config.region,
sslEnabled: true,
credentials: {
accessKeyId: config.accessKey,
secretAccessKey: config.secretKey,
},
});
}
// adopted from the (see dist/Examples.js in the library example)
async function invokeLambdaHelloWorld(payload) {
initConfig(AWS_CONFIG);
var result = await invokeLambda('helloWorld', payload);
if (result === false) {
return false;
}
return result;
}
async function onEdit(e) {
// Set a comment on the edited cell to indicate when it was changed.
const range = e.range;
// Add some checks here to determine whether or not you need to call
// the lambda function.
// Prepare the payload
const payload = {
key1: 'value1',
key2: 'value2',
key3: 'value3',
};
// Call the lambda function and get the response
const resp = await invokeLambdaHelloWorld(payload);
Logger.log(resp);
}