pythongoogle-sheetsgoogle-drive-api

How to send content as comment to a specific cell in google sheets


Using the Google Drive API and Python, I created a code that allows me to create a comment in a specific cell in Google Sheets when I type something into a form and click the submit button, but it doesn't work.

When I look at the comments tab, the comment is created, then it says [Original content deleted], and it doesn't even show that the cell is commented on.

Is there a solution? The code I created is shown below.

sheets_service = build('sheets', 'v4', credentials=credentials)
drive_service = build('drive', 'v3', credentials=credentials)

content = request.form.get('content')

comment = drive_service.comments().create(
fileId=SPREADSHEET_ID,
body={
'content': content
},
fields='id'
).execute()

Solution

  • Use Google Sheet API directly.

    from googleapiclient.discovery import build
    
    sheets_service = build('sheets', 'v4', credentials=credentials)
    content = request.form.get('content')
    
    # Define the range for the cell
    range_ = 'Sheet1!A1'
    
    comment = {
        'content': content,
        'location': {
            'sheetId': 0,
            'rowIndex': 0,
            'columnIndex': 0
        }
    }
    
    sheets_service.spreadsheets().comments().create(
        spreadsheetId=SPREADSHEET_ID,
        body=comment
    ).execute()
    

    Google Drive API will allow you to comment on file level and not cell level