Excel Spreadsheet Writer Totals

Does anyone have experience with creating totals and subtotals using Excel Spreadsheet Writer?

Im able to create totals but how do I make a subtotal which will be optional?
This is what I have so far:

public function writeTable($result, $field, $subGroup =‘’)
{
$countGroup = 0;
for($i = 0;$i < count($result);$i++) {
$this->countAll += 1;
$topGroup = $result[$i][$field];
if($subGroup <> $topGroup) {
if($subGroup <> ‘’) {
$this->writeTotals($countGroup, ‘Tl’, $subGroup);
}
$this->writeGroup($topGroup);
$subGroup = $topGroup;
$countGroup = 0;
}
$countGroup += 1;
$this->writeRow($result[$i]);
}
$this->writeTotals($countGroup, ‘Tl’, $subGroup);
$this->writeTotals($this->countAll, ‘Gt’, ‘’);
}