Skip to contents

This function connects to an Overquota database, retrieves the relevant data for a given srv_id, and syncs it to a specified Google Sheet tab. If the tab doesn't exist, it creates one. If the tab exists, it checks if the column structure matches, and either re-uploads the data or appends new data based on the last record_id.

Usage

sync_overquota_data_with_gsheet(gsheet_id, tab_name, srv_id)

Arguments

gsheet_id

Character. The ID of the Google Sheet where the data will be uploaded. This can be the full link or ID.

tab_name

Character. The name of the Google Sheet tab where the data will be synced.

srv_id

Character. The ID of the survey to be filtered in the overquotas table.

Value

None. The function performs operations on the Google Sheet directly.

Details

The function performs the following steps:

  1. Connects to the Overquota database using credentials from g6tr.db.

  2. Retrieves and filters data from the overquotas table based on srv_id, and then unpacks fields as needed.

  3. Authenticates Google Sheets API access using an encrypted JSON secret.

  4. Checks if the target tab in the Google Sheet exists:

  • If the tab doesn't exist, the function creates a new tab and uploads the data.

  • If the tab exists, the function checks if the column structure matches:

    • If the structure differs, the data in the tab is replaced.

    • If the structure matches, it filters the new data based on record_id and appends only the new data.

Please make sure 'overquota-api@g6tr-automate.iam.gserviceaccount.com' has editor access to the sheet you provide.

Examples

if (FALSE) { # \dontrun{
sync_overquota_data_with_gsheet(gsheet_id = "your-google-sheet-id",
                          tab_name = "DataTab",
                          srv_id = "survey123")
} # }