简体   繁体   English

如何使用 aws lambda 通过 python 将成本资源管理器中的数据传输到谷歌电子表格

[英]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.

相关问题 AWS 成本管理器数据不匹配 - AWS Cost Explorer data mismatch 从Boto3 Cost Explorer客户端解析AWS Lambda Python函数响应 - Parsing AWS Lambda Python Function Response from Boto3 Cost Explorer Client 如何在使用 aws lambda 和 django 时从 s3 访问谷歌电子表格 json 文件 - how to access google spreadsheet json file from s3 while using aws lambda with django 在 python 中使用 AWS LAMBDA 对 Google 采取的行动 - Action on Google with AWS LAMBDA in python 用于电子表格的Google API Explorer返回属性-如何在Python快速入门中获取属性(文件名)? - Google API Explorer for Spreadsheet returns properties - How do I get properties (filename) in Python Quickstart? Python-如何进行身份验证从AWS Lambda咨询Google Analytics(分析)? - Python - how to authenticate consult Google Analytics from AWS Lambda? Python中的Google数据传输API - Google Data Transfer API in Python 如何使用 Python 中的 Google Sheets API 在电子表格的特定选项卡中写入数据? - How to write data in a specific tab of a spreadsheet with the Google Sheets API in Python? 如何编写python脚本来操纵谷歌电子表格数据 - How to write a python script to manipulate google spreadsheet data 如何在Python中返回AWS中lambda function的二进制数据? - How to return binary data from lambda function in AWS in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM