From 16a76e511126a1ca11526cf6c5f734bab060759a Mon Sep 17 00:00:00 2001 From: Trey Foster Date: Wed, 30 Dec 2020 10:13:40 -0600 Subject: [PATCH] add some functions --- src/Resources/Folder.php | 27 +++++++++++++ src/Resources/Sheet.php | 86 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 111 insertions(+), 2 deletions(-) diff --git a/src/Resources/Folder.php b/src/Resources/Folder.php index 5b9f8ef..728c8f3 100644 --- a/src/Resources/Folder.php +++ b/src/Resources/Folder.php @@ -21,6 +21,33 @@ public function __construct(SmartsheetClient $client, array $data) $this->client = $client; } + public function createSheets(array $sheetNames, $columns = DEFAULT_COLUMNS): Folder + { + $sheets = collect($this->getSheets()); + + foreach ($sheetNames as $sheetName) { + if (is_null($sheets->firstWhere('name', $sheetName))) { + $this->createSheet($sheetName, $columns); + } + } + + return $this->client->getFolder($this->id); + } + + /** + * + * @throws SmartsheetApiException + */ + public function createSheet($name, $columns = DEFAULT_COLUMNS) + { + return $this->client->post("folders/$this->id/sheets", [ + 'json' => [ + 'name' => $name, + 'columns' => $columns + ] + ]); + } + /** * @return string */ diff --git a/src/Resources/Sheet.php b/src/Resources/Sheet.php index 6a894d2..9d9d008 100644 --- a/src/Resources/Sheet.php +++ b/src/Resources/Sheet.php @@ -28,6 +28,24 @@ public function __construct(SmartsheetClient $client, array $data) $this->client = $client; } + public function dropAndReplace(array $rows) + { + $this->dropAllRows(); + + $this->addRows($rows); + } + + public function dropAllRows() + { + foreach (collect($this->get('rows'))->chunk(100) as $chunk) { + $this->deleteRows( + $chunk + ->pluck('id') + ->toArray() + ); + } + } + public function dropAllColumnsExcept(array $columnNames) { $columnsToDelete = collect($this->columns)->filter(function ($column) use ($columnNames) { @@ -176,11 +194,20 @@ public function addRows(array $rows): object * @param array $rows * @throws Exception */ - public function updateRows(array $rows): void + public function updateRows(array $rows) { + $rowsToUpdate = []; + foreach ($rows as $id => $row) { - $this->updateRow($id, $row); + $rowsToUpdate[] = [ + 'id' => $id, + 'cells' => $this->generateRowCells($row) + ]; } + + return $this->client->put("sheets/$this->id/rows", [ + 'json' => $rowsToUpdate + ]); } /** @@ -201,6 +228,61 @@ public function updateRow($rowId, array $cells): mixed ]); } + public function replaceFirstRow(array $cells) + { + if (count($this->rows) > 0) { + $this->updateRow($this->rows[0]->id, $cells); + } else { + $this->addRows([$cells]); + } + } + + public function sync(array $rows, string $primaryColumnName = 'primary') + { + $this->replaceRows($rows, $primaryColumnName); + } + + public function replaceRows(array $cells, string $primaryColumnName) + { + if (count($this->rows) > 0) { + $rowsToUpdate = []; + + foreach ($cells as $cell) { + foreach ($this->getRows() as $row) { + if ($row->getCell($primaryColumnName) == $cell[$primaryColumnName]) { + $id = $row->getId(); + } + } + + if (isset($id)) { + $rowsToUpdate[$id] = $cell; + } + } + + $this->updateRows($rowsToUpdate); + } else { + + $this->dropAllRows(); + + $this->addRows([$cells]); + } + } + + /** + * Adds a row to the sheet + * + * @param array $cells + * @return object|array + * @throws Exception + */ + public function createRow(array $cells): object|array + { + return $this->insertRows([ + 'toBottom' => true, + 'cells' => $this->generateRowCells($cells) + ]); + } + public function getId(): string { return $this->id;