Identify User's Email automatically

Hello Quentin,
I ran into the same problem today. If you have any Python IDE (I use jupyter), it’s just a few lines of code.

import json
import requests

formId = "<your table id>"
token = "<your API token>"
data = {
    "rowSources": [
        {
            "rootFormId": formId
        }
    ],
    "columns": [
        {
            "id": "_id",
            "formula": "_id"
        },
        {
            "id": "<any column name you want to include>",
            "formula": "<any column name you want to include>"
        }
    ],
    "filter": "<if you want to limit the output>"
}

# Here we retrieve all the records for which we want to know the authors
r = requests.post("https://www.activityinfo.org/resources/query/rows", json=data, headers = {'Content-type': 'application/json', 'Accept': 'application/json', 'Authorization': f'Bearer {token}'})
records = r.json()

# For each record we request its history and take the first entry, when the record was created
for record in records:
    result = requests.get(f"https://www.activityinfo.org/resources/form/{formId}/record/{i['id']}/history", headers = {'Content-type': 'application/json', 'Accept': 'application/json', 'Authorization': f'Bearer {token}'})
    history = result.json()
    record['User'] = history['entries'][-1]['user']['name']
    record['E-mail'] = history['entries'][-1]['user']['email']

# Print all the columns separated by tabs (good for copying into a spreadsheet)
    for column in record:
        print(record[column], end = "\t")
    print("\n", end = "")

Or, if you want the output to be pretty printed, you can replace the last tree lines with this:

    print('.', end = "")
print('\n', end = "")
for record in records:
    for column in record:
        length = max((len(item[column]) for item in records), default = 0)
        f = "{:<" + str(length + 1) + "}"
        print(f.format(record[column]), end = "\t")
    print("\n", end = "")

Feel free to ask for more details if you’re not familiar with API but ready to give it a try :slight_smile:

2 Likes