Append value on Google doc through API

I want to ask few questions related to google doc :- 

  1. I want to append value on multiples line on google doc.
  2. there is a table of 3*3 and I want to add data on each cell of table in google doc.

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Lead Developer

    I asked your question to ChatGPT. It came up with some Python code which you could adapt to Appian. I did not test or verify this code.

    from google.oauth2 import service_account
    from googleapiclient.discovery import build
    
    # Replace with your own values
    DOCUMENT_ID = 'YOUR_DOCUMENT_ID'
    TABLE_INDEX = 0  # Index of the table in the document (0 for the first table)
    
    # Authenticate using service account credentials
    SCOPES = ['https://www.googleapis.com/auth/documents']
    SERVICE_ACCOUNT_FILE = 'path/to/your/service-account-file.json'
    
    credentials = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES)
    
    service = build('docs', 'v1', credentials=credentials)
    
    # Data to append to each cell
    data = [
        ['Value 1', 'Value 2', 'Value 3'],
        ['Value 4', 'Value 5', 'Value 6'],
        ['Value 7', 'Value 8', 'Value 9']
    ]
    
    # Prepare requests to update the table
    requests = []
    for i in range(len(data)):
        for j in range(len(data[i])):
            requests.append({
                'insertText': {
                    'location': {
                        'index': get_cell_index(TABLE_INDEX, i, j),
                    },
                    'text': data[i][j]
                }
            })
    
    # Execute the batch update
    result = service.documents().batchUpdate(documentId=DOCUMENT_ID, body={'requests': requests}).execute()
    print('Table updated successfully!')
    
    def get_cell_index(table_index, row, column):
        # This function calculates the index of the cell in the document
        # You may need to adjust this based on your document structure
        # This is a simplified example and may not work for all documents
        # You can use the Google Docs API to get the document structure and find the correct index
        return 1  # Replace with the actual index of the cell
    

Reply
  • 0
    Certified Lead Developer

    I asked your question to ChatGPT. It came up with some Python code which you could adapt to Appian. I did not test or verify this code.

    from google.oauth2 import service_account
    from googleapiclient.discovery import build
    
    # Replace with your own values
    DOCUMENT_ID = 'YOUR_DOCUMENT_ID'
    TABLE_INDEX = 0  # Index of the table in the document (0 for the first table)
    
    # Authenticate using service account credentials
    SCOPES = ['https://www.googleapis.com/auth/documents']
    SERVICE_ACCOUNT_FILE = 'path/to/your/service-account-file.json'
    
    credentials = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES)
    
    service = build('docs', 'v1', credentials=credentials)
    
    # Data to append to each cell
    data = [
        ['Value 1', 'Value 2', 'Value 3'],
        ['Value 4', 'Value 5', 'Value 6'],
        ['Value 7', 'Value 8', 'Value 9']
    ]
    
    # Prepare requests to update the table
    requests = []
    for i in range(len(data)):
        for j in range(len(data[i])):
            requests.append({
                'insertText': {
                    'location': {
                        'index': get_cell_index(TABLE_INDEX, i, j),
                    },
                    'text': data[i][j]
                }
            })
    
    # Execute the batch update
    result = service.documents().batchUpdate(documentId=DOCUMENT_ID, body={'requests': requests}).execute()
    print('Table updated successfully!')
    
    def get_cell_index(table_index, row, column):
        # This function calculates the index of the cell in the document
        # You may need to adjust this based on your document structure
        # This is a simplified example and may not work for all documents
        # You can use the Google Docs API to get the document structure and find the correct index
        return 1  # Replace with the actual index of the cell
    

Children
No Data