Beispiel #1
0
def add_customers(spreadsheet_id, customer_ids):
    # Read the placeholders by querying for the developer metadata we added
    # while creating the spreadsheet
    spreadsheet_reader = customer_spreadsheet_reader.CustomerSpreadsheetReader(
        sheets_service, spreadsheet_id)
    spreadsheet_reader.ReadColumnData('placeholders')
    customer_spreadsheet = spreadsheet_reader.ExecuteRead()

    sheet_id = customer_spreadsheet.GetSheetId()
    placeholders = customer_spreadsheet.GetColumnData('placeholders')

    # Process the placeholders into our query properties
    properties = []
    for p in placeholders:
        # Remove any suffix from the property name
        m = re.search(r'{(\w+)(\.\w+)*}', p)
        properties.append(m.group(1))

    data_service = customer_data_service.CustomerDataService()
    writer = spreadsheet_writer.SpreadsheetWriter(sheets_service,
                                                  spreadsheet_id)

    for customer_id in customer_ids:
        # Get the customer data from the internal customer data service
        customer_data = data_service.GetCustomerData(customer_id, properties)

        # Write the customer data to the spreadsheet
        writer.InsertColumn(sheet_id=sheet_id, column_index=1)
        writer.PopulateColumn(sheet_id=sheet_id,
                              column_index=1,
                              column_id=customer_id,
                              values=customer_data)

    writer.ExecuteBatchUpdate()
Beispiel #2
0
def create_presentations(spreadsheet_id, customer_ids):
    spreadsheet_reader = customer_spreadsheet_reader.CustomerSpreadsheetReader(
        sheets_service, spreadsheet_id)

    spreadsheet_reader.ReadColumnData('placeholders')
    for customer_id in customer_ids:
        spreadsheet_reader.ReadColumnData(customer_id)

    customer_spreadsheet = spreadsheet_reader.ExecuteRead()
    placeholders = customer_spreadsheet.GetColumnData('placeholders')

    # Get the template presentation ID and its title
    template_id = customer_spreadsheet.GetTemplateId()
    pres_reader = presentation_reader.PresentationReader(
        slides_service, template_id)
    title = pres_reader.GetTitle()

    # Generate a presentation for each customer
    for customer_id in customer_ids:
        # Create a copy of the presentation
        new_title = customer_id + ' - ' + title
        presentation_id = drive_service.files().copy(fileId=template_id,
                                                     body={
                                                         'name': new_title
                                                     }).execute().get('id')

        # Replace the placeholders with the customer data in the copy
        data = customer_spreadsheet.GetColumnData(customer_id)
        data_dict = dict(zip(placeholders, data))
        writer = presentation_writer.PresentationWriter(
            slides_service, presentation_id)
        for placeholder, value in data_dict.items():
            if re.findall(r'{(\w+).image}', placeholder):
                writer.ReplaceAllShapesWithImage(placeholder, value)
            else:
                writer.ReplaceAllText(placeholder, value)
            writer.ExecuteBatchUpdate()

        print(customer_id + ': https://docs.google.com/presentation/d/' +
              presentation_id)