[英]How to transfer data in cost explorer to google spreadsheet with aws lambda by python
I need your help as I'm stuck on aws.我需要你的帮助,因为我被困在 aws 上。
I would like to get daily cost from cost explorer in aws and then send them to google spreadsheet.我想从 aws 中的成本资源管理器获取每日成本,然后将它们发送到谷歌电子表格。
My code is able to get data from cost explorer with aws lambda, but I have no idea how to send this data to google spreadsheet.我的代码能够使用 aws lambda 从成本资源管理器获取数据,但我不知道如何将此数据发送到谷歌电子表格。
Does anyone have any idea for that?有没有人对此有任何想法?
Would be helpful for any advice!任何建议都会有所帮助!
Thank you!谢谢!
Here is a small wrapper class I created by using gspread (created it long ago, you may need to delete unwanted methods).这是我使用 gspread 创建的一个小包装类(很久以前创建的,您可能需要删除不需要的方法)。 To continue with this first you have to create credentials from google developer console .
首先,您必须从google developer console创建凭据。 You can find a guide from here .
您可以从这里找到指南。
import gspread
from oauth2client.service_account import ServiceAccountCredentials
def authentication(credentials, scope):
"""Authenticate to use gmail api service
Args:
credentials: path to the credentials json file
scope: permission levels. Should be a list
Available scope = ['https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive']
"""
credentials = ServiceAccountCredentials.from_json_keyfile_name(credentials, scope)
auth = gspread.authorize(credentials)
return auth
def open_sheet(auth, p, user_input):
"""Open google spreadsheet
Args:
auth: session created by authentication method
p: way to open the spreadsheet. available options n:- by name of sheet i:- by index of sheet u:- by the
url of the spreadsheet
user_input: spreadsheet name or id or url according to the parameter p
"""
if p == 'n':
sheet = auth.open(user_input)
return sheet
elif p == 'i':
sheet = auth.open_by_key(user_input)
return sheet
elif p == 'u':
sheet = auth.open_by_url(user_input)
return sheet
else:
return 'Parameter not identified'
def create_sheet(auth, name, share_to, role):
"""Create google spreadsheet
Args:
auth: session created by authentication methode
name: name of the sheet
"""
sh = auth.create(name)
sh.share(share_to, perm_type='user', role=role)
def find_all_cell_coordinates(sheet, n, search_key):
"""find all occurrences of particular search keyword
Args:
sheet: spreadsheet opened by open_sheet method
n: index of worksheet in the spreadsheet. Starts from 0
search_key: keyword to search
"""
lst = []
wk_sheet = sheet.get_worksheet(n)
cells = wk_sheet.findall(search_key)
for i in cells:
ll = [i.row, i.col]
lst.append(ll)
return lst
def find_cell_coordinates(sheet, n, search_key):
"""find single and first of particular search keyword
Args:
sheet: spreadsheet opened by open_sheet method
n: index of worksheet in the spreadsheet. Starts from 0
search_key: keyword to search
"""
wk_sheet = sheet.get_worksheet(n)
cell = wk_sheet.find(search_key)
lst = [cell.row, cell.col]
return lst
def write_cell_value(sheet, n, row, col, value):
"""Write on given cell
Args:
sheet: spreadsheet opened by open_sheet method
n: index of worksheet in the spreadsheet. Starts from 0
row: row number of cell
col: column number of cell
value: value to write in the cell
"""
wk_sheet = sheet.get_worksheet(n)
wk_sheet.update_cell(row, col, value)
def replace_cell_value(sheet, n, search_key, value):
"""find single occurrences of particular search keyword and replace it
Args:
sheet: spreadsheet opened by open_sheet method
n: index of worksheet in the spreadsheet. Starts from 0
search_key: keyword to search
value: value to replace
"""
row = find_cell_coordinates(sheet, n, search_key)[0]
col = find_cell_coordinates(sheet, n, search_key)[1]
write_cell_value(sheet, n, row, col, value)
def replace_all_cell_value(sheet, n, search_key, value):
"""find all the occurrences of particular search keyword and replace it
Args:
sheet: spreadsheet opened by open_sheet method
n: index of worksheet in the spreadsheet. Starts from 0
search_key: keyword to search
value: value to replace
"""
lst = find_all_cell_coordinates(sheet, n, search_key)
for i in lst:
row = i[0]
col = i[1]
write_cell_value(sheet, n, row, col, value)
def get_cell_value(sheet, n, row, col):
"""get the value of a given cell
Args:
sheet: spreadsheet opened by open_sheet method
n: index of worksheet in the spreadsheet. Starts from 0
row: row number of the cell
col: column number of the cell
"""
wk_sheet = sheet.get_worksheet(n)
value = wk_sheet.cell(row, col).value
return value
def append_row(sheet, n, row_list):
"""Append a row to the end of the worksheet
Args:
sheet: spreadsheet opened by open_sheet method
n: index of worksheet in the spreadsheet. Starts from 0
row_list: values should be in the row as a list
"""
wk_sheet = sheet.get_worksheet(n)
wk_sheet.append_row(row_list)
def get_column_total(sheet, n, col, frm, to):
"""get the total value of a particular column. User can define which row to start and end
Args:
sheet: spreadsheet opened by open_sheet method
n: index of worksheet in the spreadsheet. Starts from 0
col: column number starts form 1
frm: row to start calculation
to: row to end calculation
"""
val = 0
for i in range(frm, to + 1):
try:
add = float(get_cell_value(sheet, n, i, col))
val = val + add
except:
return 'Format Error'
return val
def get_last_row(sheet, n, col):
"""get the last row of a worksheet
Args:
sheet: spreadsheet opened by open_sheet method
n: index of worksheet in the spreadsheet. Starts from 0
col: column number starts from 1
"""
count = 1
while True:
try:
if get_cell_value(sheet, n, count, col) != '':
count = count + 1
else:
break
except:
return 'Corrupted Column. Empty cell in the middle'
return count
def insert_row(sheet, n, row, row_list):
"""insert sequence of values to a given row
Args:
sheet: spreadsheet opened by open_sheet method
n: index of worksheet in the spreadsheet. Starts from 0
row: row number to insert data, starts from 1
row_list: data sequ
"""
for i in row_list:
write_cell_value(sheet, n, row, row_list.index(i)+1, i)
def create_chart():
requests = []
requests.append({
'updateSpreadsheetProperties': {
'properties': {
'title': title
},
'fields': 'title'
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.