Google Sheets API两次插入记录(重复记录) [英] Google Sheets API inserting records twice (duplicated records)
问题描述
我目前正在使用Google::Apis::Sheets
处理Ruby脚本应用程序.我遇到了一个问题,试图找出从哪里生成的.基本上,当将数据追加到Google工作表中时,数据将被插入两次.因此,例如,如果将50条记录添加到Google表格中,则会创建100条记录.
I'm currently working on a Ruby script application using Google::Apis::Sheets
. I'm encountering an issue that I'm trying to figure out where is being generated from. Basically when data is append into the Google sheet the data is being inserted twice. So for example, if 50 records are pass to be appended into the Google Sheet, 100 records are created.
# data should be an array of arrays
class Sheets
#some code here
def append(data)
# Initialize the API
service = Google::Apis::SheetsV4::SheetsService.new
service.client_options.application_name = APPLICATION_NAME
service.authorization = authorize
# Prints the names and majors of students in a sample spreadsheet:
# https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
spreadsheet_id = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
range = "Sheet1!A:C"
request_body = Google::Apis::SheetsV4::ValueRange.new
request_body.values = data
service.append_spreadsheet_value(spreadsheet_id, range, request_body, value_input_option: 'USER_ENTERED')
end
end
上述方法 append_spreadsheet_value 将这些值附加到电子表格中.我试图找出将data
传递到request_body.values
时错误是来自range
还是request_body
.
The above method append_spreadsheet_value appends the values into the spreadsheet. I'm trying to figure out if the error is coming from the range
or request_body
when data
is passed into the request_body.values
.
正在从托管在AWS Lambda上的另一个文件reporting.rb
调用方法append
.该方法具有以下脚本.
The method append
is being called from another file reporting.rb
which is hosted on AWS Lambda. The method has the following script.
def self.process(event:, context:, db: MySQL.new)
FileUtils.cp('./token.yaml', '/tmp/token.yaml')
last_day = db.get_last_day()
sheets = Sheets.new
data = []
last_day.each do |row|
data.push([row["created_at"].strftime("%Y-%m-%d"), row["has_email"], row["type"]])
end
sheets.append(data)
api_gateway_resp(statusCode: 204)
end
基本上在方法last_day
中,我正在通过MySQL2
客户端从数据库中检索一些记录.然后,我遍历last_day
,并将每个rwo添加到data
中.因此,基本上data
是一个数组数组,用于保存以下格式的记录.
Basically in the method last_day
I'm retrieving some records from a DB via a MySQL2
client. I then iterate over last_day
and I add each rwo into data
. So basically data
is an array of arrays holding the records in the following format.
data = [
[2020-08-06, 1, QUARANTINE],
[2020-08-06, 1, QUARANTINE],
[2020-08-06, 1, POSITIVE],
[2020-08-06, 1, POSITIVE],
[2020-08-06, 1, POSITIVE],
[2020-08-06, 1, QUARANTINE],
[2020-08-06, 0, POSITIVE],
[2020-08-06, 1, QUARANTINE]
]
因此,如果将sheets.append(data)
数据附加到Google绵羊时,data
有10条记录,则会创建20条记录.
So if data
has 10 records when sheets.append(data)
data is append to the Google sheep 20 records are created.
推荐答案
该问题是由于Lambda函数触发器的配置设置为每10小时运行一次.
The issue was due to the configuration of the Lambda function trigger being set to run every 10 hours.
这篇关于Google Sheets API两次插入记录(重复记录)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!