← All articles

Data Integration

Excel Online Data Integration

Read and write spreadsheet data via Microsoft Graph API with inline data editors embedded in Drupal pages and scheduled exports.

Published February 10, 2024
drupalexcel-onlinedata-integrationspreadsheetsautomationoffice365reporting

Overview

Excel Online integration for Drupal bridges the gap between structured spreadsheet data and your content management system. This powerful solution enables real-time data synchronization, embedded spreadsheet editors, and automated reporting workflows using Microsoft Graph API.

Features

  • Read and write Excel data via Graph API
  • Inline spreadsheet editor embedded in Drupal
  • Automated data import and export
  • Scheduled exports for reporting
  • Cell-level data validation
  • Formula preservation and calculation
  • Multiple worksheet support
  • Excel template management

Technical Details

Technologies: Microsoft Graph API, Excel Online API, Drupal Field API, Drupal Entity API, Drupal Cron, JavaScript Office.js

Requirements:

  • Drupal 9.x or 10.x
  • Microsoft 365 with Excel Online
  • Azure AD app with Files.ReadWrite permission
  • OneDrive or SharePoint for file storage
  • Modern browser with JavaScript enabled

API Endpoints:

Get worksheet: GET /me/drive/items/{item-id}/workbook/worksheets/{sheet-name}
Get range: GET /me/drive/items/{item-id}/workbook/worksheets/{sheet-name}/range(address='{range}')
Update range: PATCH /me/drive/items/{item-id}/workbook/worksheets/{sheet-name}/range(address='{range}')
Add table: POST /me/drive/items/{item-id}/workbook/tables/add

Implementation

  1. Configure Azure AD app with Excel API permissions
  2. Create custom field type for Excel data
  3. Implement Graph API client for Excel operations
  4. Build inline editor widget with Office.js
  5. Create import/export service
  6. Set up scheduled cron jobs for exports
  7. Implement data validation and transformation
  8. Add template management system
  9. Configure range mapping for data sync
  10. Build admin UI for Excel file selection
<?php
// Example: Excel data import service
namespace Drupal\excel_integration\Service;

use Microsoft\Graph\Graph;
use Microsoft\Graph\Model;

class ExcelDataImporter {
  
  protected $graphClient;
  
  public function __construct($graph_client) {
    $this->graphClient = $graph_client;
  }
  
  public function importRange($file_id, $sheet_name, $range) {
    try {
      // Get range data from Excel
      $endpoint = "/me/drive/items/{$file_id}/workbook/worksheets/{$sheet_name}/range(address='{$range}')";
      $response = $this->graphClient->createRequest('GET', $endpoint)
        ->setReturnType(Model\WorkbookRange::class)
        ->execute();
      
      // Transform Excel data to Drupal entities
      $values = $response->getValues();
      $entities = [];
      
      // Skip header row
      for ($i = 1; $i < count($values); $i++) {
        $row = $values[$i];
        $entity = \Drupal::entityTypeManager()
          ->getStorage('node')
          ->create([
            'type' => 'imported_data',
            'title' => $row[0],
            'field_value_1' => $row[1],
            'field_value_2' => $row[2],
            'field_value_3' => $row[3],
          ]);
        $entity->save();
        $entities[] = $entity;
      }
      
      \Drupal::logger('excel_integration')->info(
        'Imported @count rows from Excel',
        ['@count' => count($entities)]
      );
      
      return $entities;
      
    } catch (\Exception $e) {
      \Drupal::logger('excel_integration')->error(
        'Excel import failed: @message',
        ['@message' => $e->getMessage()]
      );
      throw $e;
    }
  }
  
  public function exportToExcel($entities, $file_id, $sheet_name, $start_cell = 'A2') {
    $data = [];
    foreach ($entities as $entity) {
      $data[] = [
        $entity->label(),
        $entity->get('field_value_1')->value,
        $entity->get('field_value_2')->value,
        $entity->get('field_value_3')->value,
      ];
    }
    
    // Update Excel range
    $endpoint = "/me/drive/items/{$file_id}/workbook/worksheets/{$sheet_name}/range(address='{$start_cell}')";
    $this->graphClient->createRequest('PATCH', $endpoint)
      ->attachBody(['values' => $data])
      ->execute();
      
    return count($data);
  }
}

Use Cases

  • Financial data reporting and dashboards
  • Product inventory management
  • Survey data collection and analysis
  • Budget tracking and forecasting
  • Data migration and bulk updates

Benefits

  • Leverage Excel’s powerful calculation engine
  • Familiar spreadsheet interface for users
  • Real-time data synchronization
  • Automated reporting workflows
  • Reduced manual data entry errors
  • Version control through OneDrive/SharePoint
  • Collaborative editing with Office 365 users