Extend Google Sheets with IBM Cloud Object Storage Data



Make calls to the API Server from Google Apps Script.

Interact with IBM Cloud Object Storage data from Google Sheets through macros, custom functions, and add-ons. The CData API Server enables connectivity to IBM Cloud Object Storage data from cloud-based and mobile applications like Google Sheets. The API Server is a lightweight Web application that produces OData services for IBM Cloud Object Storage.

Google Apps Script can consume these OData services in the JSON format. This article shows how to create a simple add-on that populates a Google Spreadsheet with Objects data.

Set Up the API Server

If you have not already done so, download the CData API Server. Once you have installed the API Server, follow the steps below to begin producing secure IBM Cloud Object Storage OData services:

Connect to IBM Cloud Object Storage

To work with IBM Cloud Object Storage data from Google Sheets, we start by creating and configuring a IBM Cloud Object Storage connection. Follow the steps below to configure the API Server to connect to IBM Cloud Object Storage data:

  1. First, navigate to the Connections page.
  2. Click Add Connection and then search for and select the IBM Cloud Object Storage connection.
  3. Enter the necessary authentication properties to connect to IBM Cloud Object Storage.

    Register a New Instance of Cloud Object Storage

    If you do not already have Cloud Object Storage in your IBM Cloud account, follow the procedure below to install an instance of SQL Query in your account:

    1. Log in to your IBM Cloud account.
    2. Navigate to the page, choose a name for your instance and click Create. You will be redirected to the instance of Cloud Object Storage you just created.

    Connecting using OAuth Authentication

    There are certain connection properties you need to set before you can connect. You can obtain these as follows:

    API Key

    To connect with IBM Cloud Object Storage, you need an API Key. You can obtain this as follows:

    1. Log in to your IBM Cloud account.
    2. Navigate to the Platform API Keys page.
    3. On the middle-right corner click "Create an IBM Cloud API Key" to create a new API Key.
    4. In the pop-up window, specify the API Key name and click "Create". Note the API Key as you can never access it again from the dashboard.

    Cloud Object Storage CRN

    If you have multiple accounts, specify the CloudObjectStorageCRN explicitly. To find the appropriate value, you can:

    • Query the Services view. This will list your IBM Cloud Object Storage instances along with the CRN for each.
    • Locate the CRN directly in IBM Cloud. To do so, navigate to your IBM Cloud Dashboard. In the Resource List, Under Storage, select your Cloud Object Storage resource to get its CRN.

    Connecting to Data

    You can now set the following to connect to data:

    • InitiateOAuth: Set this to GETANDREFRESH. You can use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the OAuthAccessToken.
    • ApiKey: Set this to your API key which was noted during setup.
    • CloudObjectStorageCRN (Optional): Set this to the cloud object storage CRN you want to work with. While the connector attempts to retrieve this automatically, specifying this explicitly is recommended if you have more than Cloud Object Storage account.

    When you connect, the connector completes the OAuth process.

    1. Extracts the access token and authenticates requests.
    2. Saves OAuth values in OAuthSettingsLocation to be persisted across connections.
  4. After configuring the connection, click Save & Test to confirm a successful connection.

Configure API Server Users

Next, create a user to access your IBM Cloud Object Storage data through the API Server. You can add and configure users on the Users page. Follow the steps below to configure and create a user:

  1. On the Users page, click Add User to open the Add User dialog.
  2. Next, set the Role, Username, and Privileges properties and then click Add User.
  3. An Authtoken is then generated for the user. You can find the Authtoken and other information for each user on the Users page:

Creating API Endpoints for IBM Cloud Object Storage

Having created a user, you are ready to create API endpoints for the IBM Cloud Object Storage tables:

  1. First, navigate to the API page and then click Add Table .
  2. Select the connection you wish to access and click Next.
  3. With the connection selected, create endpoints by selecting each table and then clicking Confirm.

Gather the OData Url

Having configured a connection to IBM Cloud Object Storage data, created a user, and added resources to the API Server, you now have an easily accessible REST API based on the OData protocol for those resources. From the API page in API Server, you can view and copy the API Endpoints for the API:

Retrieve IBM Cloud Object Storage Data

Open the Script Editor from your spreadsheet by clicking Tools -> Script Editor. In the Script Editor, add the following function to populate a spreadsheet with the results of an OData query:


function retrieve(){
  var url = "https://MyUrl/api.rsc/Objects?select=Id,Key,Etag,Bucket";
  var response = UrlFetchApp.fetch(url,{
    headers: {"Authorization": "Basic " + Utilities.base64Encode("MyUser:MyAuthtoken")}
  }); 
  var json = response.getContentText();
  var sheet = SpreadsheetApp.getActiveSheet();
  var a1 = sheet.getRange('a1');
  var index=1;
  var objects = JSON.parse(json).value;

  var cols = [["Id","Key","Etag","Bucket"]]; 
  sheet.getRange(1,1,1,4).setValues(cols);

  row=2;
  for(var i in objects){
    for (var j in objects[i]) {
      switch (j) {
        case "Id":
          a1.offset(row,0).setValue(account[i][j]);
          break;
        case "Key":
          a1.offset(row,1).setValue(account[i][j]);
          break;
        case "Etag":
          a1.offset(row,2).setValue(account[i][j]);
          break;
        case "Bucket":
          a1.offset(row,3).setValue(account[i][j]);
          break;
      }      
    }
    row++;
  }
}

Follow the steps below to add an installable trigger to populate the spreadsheet when opened:

  1. Click Resources -> Current Project's Triggers -> Add a New Trigger.
  2. Select retrieve in the Run menu.
  3. Select From Spreadsheet.
  4. Select On open.

After closing the dialog, you are prompted to allow access to the application.

You can test the script by clicking Publish -> Test as Add-On. Select the version, installation type, and spreadsheet to create a test configuration. You can then select and run the test configuration.

Ready to get started?

Learn more or sign up for a free trial:

CData API Server