Categories
Data Engineering DataStudio

Adding Images to DataStudio from GDrive

Short version:

  1. Get the unique ID of the Image in GDrive by ‘Get Link’, making it publicly available, and copying the text between ..file/d/ and /view

2. Use the ID to generate a ‘download’ url e.g.

https://drive.google.com/uc?export=download&id=YOUR-FILES-ID

3. Add a Calculated field inside of DataStudio with the Image Function

Image('https://drive.google.com/uc?export=download&id=YOUR-FILES-ID')

4. Use it in a Table Component

Long Version

Getting all the Images from one GDrive folder, and programmatically using them in a DataStudio table.

For this example we’ll use Country Flags, and show them in a table with Google Analytics Visits data to show visits by country.

  1. Download all the images of flags for each country for free from https://flagpedia.net/download/icons – i went with the 40px ones

2. Create a folder in your GDrive called ‘public’, make it publicaly available, and upload all the files from the downloaded zip into it.

3. Get the unique id for the Gdrive folder

4. Create a new Spreadsheet listing all the filenames and their unique Gdrive id by using an App Script

  • Create a new Google Sheet document from the Google account for which you want a Google Drive directory listing.
  • In the toolbar for the new Google Sheet, go to Tools -> Script editor
  • Replace any sample code in the editor with the code below adding your Folder Id from point 3
function listFolderContents() {
  var folderid = 'YOUR-FOLDER-ID'
  var folderlisting = 'contents of folder ' + folderid;
  var folder = DriveApp.getFolderById(folderid)
  var contents = folder.getFiles();
  var ss = SpreadsheetApp.create(folderlisting);
  var sheet = ss.getActiveSheet();
  sheet.appendRow( ['name', 'link'] );
  
  var file;
  var name;
  var link;
  var row;
  while(contents.hasNext()) {
    file = contents.next();
    name = file.getName();
    link = file.getUrl();
    downloadLink = link.replace(/https:\/\/drive\.google\.com\/file\/d\/(.*?)\/.*?\?usp=drivesdk/g, "https://drive.google.com/uc?export=download&id=$1");
    sheet.appendRow( [name, downloadLink] );     
  }  
};
  • Save and then Run by using the buttons on the bar
  • ‘Authorization required ‘ pop up will appear; grant authorization.
  • A listing of the files in the specified folder, along with URLs for those files, will then appear in your My Drive as a Google Sheet named listing of folder {your-folder-id}

5. Add the GSheet as a DataSource in DataStudio

6. convert the filename ‘name’ into a ISO Country code by adding a new field

SUBSTR(UPPER(name),0,2)

7. Create another field to convert the ‘link’ to the flag image to an Image

8. Add a Google Analytics Account as a 2nd DataSource

9. Create a Blended Data Source joined on Country code

10. Show in a Table

Success!

Imagine instead of Flags you used the Logos for each of your Clients.