Loko
Loko

Reputation: 195

Empty google worksheet before writing in it

I use the asimlqt/php-google-spreadsheet-client to read google spreadsheets and to write in google spreadsheets.

There are functions to update and delete the actual worksheet but not to empty the worksheet before i write data in it.
Knows somebody who use this library a way to empty the worksheet before writing in? If not you can not be sure that old data is still available

Upvotes: 1

Views: 476

Answers (1)

Alberto Favaro
Alberto Favaro

Reputation: 1840

My personal solution is delete the worksheet and recreate

// service
$spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
$spreadsheetFeed = $spreadsheetService->getSpreadsheetFeed();

// retrieve a list of worksheets from a spreadsheet 
$spreadsheet = $spreadsheetFeed->getByTitle('Spreadsheet');
$worksheetFeed = $spreadsheet->getWorksheetFeed();

// get the worksheet you want to empty
$worksheet = $worksheetFeed->getByTitle('EmptySheet');

// delete the worksheet
$worksheet->delete();

// recreate the worksheet to be sure it's empty
$spreadsheet->addWorksheet('EmptySheet', rows, column);

Edit

Or use the batchUpdate with empty param userEnteredValue. Look at https://developers.google.com/sheets/api/samples/sheet#clear_a_sheet_of_all_values_while_preserving_formats

Upvotes: 1

Related Questions