public function save($data, Sheet $sheet) { $sheetConfig = $sheet->getConfig(); $tmpFilename = $this->writeRawCsv($data, $sheet); $dataProcessor = new DataProcessor($tmpFilename, $sheetConfig); $outFilename = $dataProcessor->process(); $this->configuration->initDataBucket($sheetConfig['db']['table']); $outputTable = $sheetConfig['db']['table']; $tableNameArr = explode('.', $outputTable); if (count($tableNameArr) != 3) { throw new UserException(sprintf("Error in configuration. Wrong tableId format '%s'", $outputTable)); } $table = new Table($this->configuration->getStorageApi(), $outputTable, $outFilename); try { $table->save(true); } catch (ClientException $e) { throw new UserException($e->getMessage(), $e, ['outputTable' => $outputTable, 'sheet' => $sheet->toArray()]); } unlink($tmpFilename); }
public function addSheet(Sheet $sheet) { $origSheet = $this->getSheet($sheet->getGoogleId(), $sheet->getSheetId()); if (null == $origSheet) { $sheet->setAccount($this); $fileIds = array(); /** @var Sheet $savedSheet */ foreach ($this->sheets as $savedSheet) { $gid = $savedSheet->getGoogleId(); if (!isset($fileIds[$gid])) { $fileIds[$gid] = $savedSheet->getFileId(); } } $nextFileId = 0; if (!empty($fileIds)) { if (isset($fileIds[$sheet->getGoogleId()])) { $nextFileId = $fileIds[$sheet->getGoogleId()]; } else { $nextFileId = max($fileIds) + 1; } } $tableName = $nextFileId . '-' . $this->removeSpecialChars($sheet->getSheetTitle()); $sheet->setFileId($nextFileId); if ($sheet->getConfig() == null) { $sheet->setConfig(array('header' => array('rows' => 1), 'db' => array('table' => $this->getInBucketId() . '.' . $tableName))); } } else { // Update Sheet $this->removeSheet($origSheet->getFileId(), $origSheet->getSheetId()); $origSheet->fromArray($sheet->toArray()); $sheet = $origSheet; } $this->sheets[] = $sheet; }