Skip to content

Commit

Permalink
add some functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Infamoustrey committed Dec 30, 2020
1 parent edbb28f commit 16a76e5
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 2 deletions.
27 changes: 27 additions & 0 deletions src/Resources/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
86 changes: 84 additions & 2 deletions src/Resources/Sheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
]);
}

/**
Expand All @@ -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;
Expand Down

0 comments on commit 16a76e5

Please sign in to comment.