JJpajama
JJpajama

Reputation: 19

Is there a API call to replace a sheet?

I was wondering if there is a API call for Google Sheets where I can delete all cells in a given sheet and paste/replace with a new data. I have the new data in the form of its own sheet. thanks!

Upvotes: 1

Views: 543

Answers (2)

Fernando Lara
Fernando Lara

Reputation: 2416

Update cell values with Google Sheets API

Since you mentioned that you need an API call for this, I think your way to go is to use the batchUpdate method from the Google Sheets API as it is the closest thing to what you are looking for.

With the batchUpdate method you can send a UpdateCellsRequestin order to replace the information from the cells you are looking for. I will leave the official documentation below so that you can check what I just mentioned.

References:

Upvotes: 1

Argyll
Argyll

Reputation: 9875

For a large existing file, it seems most expedient to delete the file and create a new one with Apps Script.

For delete, you can use DriveApp. Look for methods such as .getFileById(). Or, if you prefer to programmatically search for a filename in a folder, look for .getFolderById() and .getFilesByName(). Once you get the file object, you can either use Drive API's .remove() as shown in this answer or use DriveApp's .File.setTrashed. But note the latter moves file to trash bin, not literally remove from drive.

For file creation, you can use Advanced Drive Service. see this answer for exact steps.

As for populating the new sheet, the OP implies that step is already understood. (No particular data format was provided.) So we are done!

Upvotes: 0

Related Questions