[Python] How to get index out/NEXT to table after inserting table using google docs API python

Discussão em 'Python' iniciado por Stack, Abril 9, 2025.

  1. Stack

    Stack Membro Participativo

    I'm facing an issue I'm working on creating a google doc using google docs API in python. I read the doc: https://tanaikech.github.io/2019/05...sing-google-docs-api-with-google-apps-script/ to create a table and it worked well. But issue started after that when I tried to enter text/move index next to table, I'm getting issue that whatever i do whatever index i choose, it ends up in one of the cells in the table or I get index out of range error. :( Please help me out with this guys please. How can i move index next to table.

    Below is the code I'm using:

    # Add the table for assessment score and stage
    requests.append({"insertTable": {"rows": 6, "columns": 2, "location": {"index": index}}})

    # Insert the rows for the table
    requests.append({"insertText": {"text": "Severe dementia", "location": {"index": index + 31}}})
    requests.append({"insertText": {"text": "21-30", "location": {"index": index + 29}}})

    requests.append({"insertText": {"text": "Moderate dementia", "location": {"index": index + 26}}})
    requests.append({"insertText": {"text": "13-20.5", "location": {"index": index + 24}}})

    requests.append({"insertText": {"text": "Mild dementia", "location": {"index": index + 21}}})
    requests.append({"insertText": {"text": "6-12.5", "location": {"index": index + 19}}})

    requests.append({"insertText": {"text": "Mild cognitive impairment", "location": {"index": index + 16}}})
    requests.append({"insertText": {"text": "2-5.5", "location": {"index": index + 14}}})

    requests.append({"insertText": {"text": "Normal (dementia unlikely)", "location": {"index": index + 11}}})
    requests.append({"insertText": {"text": "0-1.5", "location": {"index": index + 9}}})

    # Insert the header row
    requests.append({"insertText": {"text": "Dementia Stage", "location": {"index": index + 6}}})
    requests.append({
    "updateTextStyle": {
    "range": {
    "startIndex": index + 6,
    "endIndex": index + 6 + len("Dementia Stage")
    },
    "textStyle": {
    "bold": True
    },
    "fields": "bold"
    }
    })

    requests.append({"insertText": {"text": "QDRS Score", "location": {"index": index + 4}}})
    requests.append({
    "updateTextStyle": {
    "range": {
    "startIndex": index + 4,
    "endIndex": index + 4 + len("QDRS Score")
    },
    "textStyle": {
    "bold": True
    },
    "fields": "bold"
    }
    })

    # Return the modified requests and updated index
    return requests, index


    I tried appending the index from index+=31 to index+=70 etc but it just moves the next next i put in one of the cells.

    Continue reading...

Compartilhe esta Página