I'm facing an issue within updating of existing spreadsheet on google drive, without using a batch update (right now I'm not sure whether it is even possible)
I already have existing spreadsheet with some data, then I retrieve file from google drive via
Drive drive = new Drive.Builder(TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
drive.files().list().execute().getItems()
I easily match my file by the name and then I just want to update the spreadsheet in the founded file.
Sheets service = new Sheets.Builder(GoogleNetHttpTransport.newTrustedTransport(), JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
Spreadsheet spreadsheet = service.spreadsheets().get(file.getId()).execute().setSheets(new ArrayList<>());
Here I get spreadsheet from file which match the name and then I just want to put there empy sheet (for example).
There is first point, where I'm not sure how to correctly "update" the spreadsheet on drive, because I cannot use
service.spreadsheets().create(spreadsheet).execute();
and I don't want to use batch update, because I already have set the spreadsheet content via setSheet() method.
I think it should be finalized with following:
Permission permission = drive.permissions().get(file.getId(), permissionId).execute().setType("anyone");
drive.permissions().update(
spreadsheet.getSpreadsheetId(),
permissionId,
permission
).execute();
I was checking the google API here:
https://developers.google.com/drive/v2/reference/permissions/update
and here:
https://developers.google.com/sheets/api/guides/batchupdate
but they are using batch update.
What I am curious is why there is setSheets() method, which seems to be easily to use for updates, when I already created my sheets, but it seems that I just cannot simply update spreadsheet in existing file.
What is also interesting that every time I use the drive.perm.update, it creates a new file(with spreadsheet that I wanted to update) which actually doesn't make much sense to me.
So is there any chance to simply update file on drive and just use the spreadsheet method?
This statement by Jay Lee in this related SO post will give a clear insight:
The Google Drive API is meant for generic file / folder interactions. For directly modifying Google Spreadsheet data, the Google Sheets API would be the better choice.
Furthermore, to update Google spreadsheet, you may want to check Reading & Writing Cell Values which describes the basics of using the spreadsheets.values collection. It was mentioned that,
If you need to update formatting or other properties in a sheet, you will need to use the spreadsheets collection, which is described in Updating Spreadsheets.
For more information, please see Batch update operations and examples are shown here.
Related
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!
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:
Method: batchUpdate
Reading & Writing Cell Values
UpdateCellsRequest
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!
I need to allow user upload excel (xlsx) file and then generate a Google Sheets file using Google Scripts (the generated file uses the input file values, process them with certain formulas and provides another excel file as output based on all processing)
I am currently doing the task in a simple desktop application (using MS Excel etc.), but problem is I have admin rights and I want other people to do the same on their PCs but they dont have admin rights, so please help me with this, whats the best way I can implement it via Google Sheets. I need following to do:
Get an XLSX file from user
Read two columns using google sheet script.
Process the read values and create a new google sheet file with 6/7 columns
Plz help as I am not looking for a ready made code but a head start, as I have never worked with google scripts before.
First you need to study the basics of Apps Script in general, and of the SpreadsheetApp, DriveApp and Advanced Drive Service in specific.
The steps to write your code would be
retrieving the excel file on your Google Drive
retrieving its blob
creating a new file of the mimeType GOOGLE_SHEETS with the contents of your blob
Once you create a Google Spreadsheet - process it with the SpreadsheetApp methods to delete spare contents/ create a copy with only the desired contents.
Here you can find useful samples.
Note: It might make sense to retrieve the columns of interest before
converting them to Google Sheets, but this is not something you can do
with Apps Script or a Google API since they do not have methods to
edit Excel files.
I'm working on some .Net code to collect some data on a server and put it in a Google Spreadsheet. The job needs to run once a day, clear out the sheet and repopulate it. I have all that working, but I can't get the authorization right. I can do it with my personal Google credentials, and I can get it to work with OAUTH by allowing the app to manipulate all my spreadsheets using the https://spreadsheets.google.com/feeds scope. But, what I really want to do is allow my script to read and write just one sheet. Is that possible, and how would I do that?
You are using very old scopes. Look at the drive.file scope
https://developers.google.com/drive/web/scopes
Its not possible using the spreadsheets API. That api is very old and doesnt have a scope for indivdual files.
it might be possible by creating a spreadsheet using the drive API by uploading a CSV with conversion, but you will need permission to create new drive files and that also means permission to the entire drive.
Workaround: Create a new google account and share the spreadsheet with it. Do the OAuth using the new account.
#Zig Mandel is right - the API (Gdata style) wants access to all spreadsheets. But you can workaround the problem using a new account.
We've previously been getting exports from the google spreadsheet api using the following url
https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=spreadsheetID&exportFormat=csv&gid=gid
When Google introduced the new Sheets this URL did not work for them, though did continue to work for old style spreadsheets. As a result we've been using the Google drive API to get the new style sheets, but this has some limitations as you must get the whole spreadsheet instead of an individual worksheet and it does not support CSV(your exports must be Excel).
Though after reading the following question : Is an API available for the new Google sheets (spreadsheets)? It looks like the new sheets are supported despite no information about it in the api documentation.
After digging through the response I found the new sheets have an export link included as field in the format:
https://docs.google.com/spreadsheets/d/spreadsheetID/export?format=csv&gid=gid
Which works great. The problem is that the old sheets don't work with the new URL andthere is no field specifying that it is a new sheet and it must be inferred from the existence of the field.
So I'm wondering if there is a better way to determine if I'm dealing with an old or new sheet and if this is a reliable way to get spreadsheet data as it doesn't seem to be documented.
Thanks!
Google spreadsheet API (GData style) works the same for both old and new style sheets.
Possible issues:
1) non public sheets require OAuth2
2) The sheet id is not the GID. You need the GData style sheet ID to get the sheet. So it needs an API call to get the mappings from sheet name to sheet id
3) The raw format of the data is XML
I have a problem in my reporting, i create every day a google doc tracker where all the stack holders in my department update their work progress in it, so i have plenty of spreadsheets to monitor which is a hassle, here is what im trying to do, I'm trying to create a big google doc tracker where i can have an access over date applied in the normal spreadsheets, all what i need is the spreadsheet's URLs that exist in my google drive to be retrieved in this big tracker, with this i'll be able to drive all the needed data from the normal trackers.
PS: I'm not good with google scripts.
You can use the Drive Service to get a list of files with MIME type "application/vnd.google-apps.spreadsheet" using getFilesByType. This returns a FileIterator, which you can use to individually get each Spreadsheet file. From there, just use getUrl() to find the URL's. The FileIterator link has examples of how to loop through all the matching files.