Skip to main content
rishukumarg1965
January 10, 2025
Question

Append value on Google doc through API

  • January 10, 2025
  • 4 replies
  • 0 views

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.

4 replies

rishukumarg1965
January 10, 2025

[mention:b8ea18cfbb2e4f6d841cf3c1d4d7420b:e9ed411860ed4f2ba0265705b8793d05] [mention:2df2892a4df7486f9775b4d2f0e405fd:e9ed411860ed4f2ba0265705b8793d05] 

rishukumarg1965
January 13, 2025
  • I'm able to create the table but not able to append values on every cell of the table. 
stefanhelzle0001
January 13, 2025

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