Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download a Google Docs Spreadsheet as HTML

Im using the Google Drive api with php to do all sorts of wonderful things but getting stuck with downloading a Spreadsheet in HTML format. I can use the getExportLinks() method to download it in its various formats, but all i want is the text from the sheet and not a formatted file in docx or ods.

This was possible with the google docs api by using the /feeds/download/spreadsheets/Export?exportFormat=html&format=html&key=ID url. But currently being redirected when I use it.

Is it possible to download a spreadsheet as html using the drive api?

like image 891
Andre J Avatar asked Dec 27 '25 16:12

Andre J


2 Answers

@Andre J's answer works, the code:

Drive driveService = new Drive.Builder(TRANSPORT, JSON_FACTORY, credential).build();
File file = driveService.files().get(____documentKey____).execute();
String downloadUrl = file.getExportLinks().get("application/pdf");
downloadUrl = downloadUrl.replaceFirst("exportFormat=pdf", "exportFormat=html");
HttpResponse resp = driveService.getRequestFactory().buildGetRequest(new GenericUrl(downloadUrl))
    .execute();

System.out.println("downloadUrl: " + downloadUrl);
InputStream fileContent = resp.getContent();
String htmlContent = new Scanner(fileContent).useDelimiter("\\A").next();

System.out.println("htmlContent: " + htmlContent);
like image 74
eddyparkinson Avatar answered Dec 30 '25 06:12

eddyparkinson


Just change the exportformat in the exportLink to html.

like image 26
Andre J Avatar answered Dec 30 '25 06:12

Andre J



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!