Reading xml-files on Google Drive using Apps Script - google-apps-script

I have some XML-files saved on Google Drive. I want to save their content into a spreadsheet.
Google Apps Script should be the tool:
DriveApp locates files.
XmlService reads XML-files.
SpreadsheetApp stores the content.
I don't know how to read the content from a XML-file saved on Drive.
DriveApp gives a downloadUrl but GAS doesn't allow downloading files.
UrlFetchApp isn't authenticated.
It it impossible or is there a workaround?

here a little code that may help you:
function xmlRead(){
var id = "ID_OF_THE_FILE";
var rawXml = DriveApp.getFileById(id).getBlob().getDataAsString();
var xml = XmlService.parse(rawXml).;
// then it's your job to extract what you want from this XML
var ssid = "ID_OF_THE_SPREADSHEET";
var ss = SpreadsheetApp.openById(ssid);
ss.appendRow(["some stuff you extracted"]);
}

Related

How to get XML File parsed from Google Drive without sharing the Link globaly

I'm building a XML parser in Google App Script, witch gets XMLs from Google Drive. At the moment i could only get it to work, when the link of the XML file is shared global.
function XmlChecker(FileID){
var data = UrlFetchApp.fetch("https://drive.google.com/uc?id=" + FileID + "&export=download").getContentText();
var xml = XmlService.parse(data);
var root = xml.getRootElement();
var namespace = XmlService.getNamespace('http://www.editeur.org/onix/2.1/short');
Is there a way to get the same result without sharing the link?
The Google App Scripts is build in a Spreadsheet. Can i give the spreadsheet access via code?
If the file of FileID is in your Google Drive, how about retrieving the data using Drive service as follows?
From:
var data = UrlFetchApp.fetch("https://drive.google.com/uc?id=" + FileID + "&export=download").getContentText();
To:
var data = DriveApp.getFileById(FileID).getBlob().getDataAsString();
By this modification, the data can be retrieved from the file of FileID without publicly sharing.
Reference:
Drive Service

How to extract the pubhtml ID in google sheet using Apps Script?

I'm trying to get the webpage link of my google sheet using Apps Script. When you go to the File>Publish to the web>publish, you will see a weblink there. I want to get that link via apps script. Please help.
You want to retrieve the URL like https://docs.google.com/spreadsheets/d/e/2PACX-###/pubhtml using Google Apps Script.
If my understanding is correct, how about this answer?
Issue and workaround:
File>Publish to the web>publish can be run for the Google Docs. This URL could been retrieved with publishedLink of Drive API v2 before. But in the current stage, unfortunately, this cannot be used for both Drive API v2 and v3. So as a workaround, I would like to propose to use file ID.
By the way, 2PACX-### of https://docs.google.com/spreadsheets/d/e/2PACX-###/pubhtml is not the file ID. Also please be careful this.
Sample script:
In this case, as a test case, the URL of published Spreadsheet is retrieved.
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var url = ss.getUrl().replace("edit", "pubhtml"); // or replace("edit", "pub");
Logger.log(url)
}
Note:
At above script, when the Spreadsheet is not published to web, when you access to the URL, the Spreadsheet is not shown. Please be careful this.
For example, if you want to retrieve the URL of Google Document, you can use the following script. Unfortunately, at Google Document, getUrl() returns https://docs.google.com/open?id=###. So above script cannot be used.
function myFunction() {
var doc = DocumentApp.getActiveDocument();
var url = "https://docs.google.com/document/d/" + doc.getId() + "/pub";
Logger.log(url)
}
References:
V2 to v3 reference
getUrl()
If I misunderstood your question and this was not the direction you want, I apologize.
Added:
If you can enable Drive API at Advanced Google Services, you can also use the following script.
Pattern 1:
When you use the container-bound script of Google Docs, you can use this.
var url = Drive.Files.get((DocumentApp.getActiveDocument() || SpreadsheetApp.getActiveSpreadsheet() || SlidesApp.getActivePresentation()).getId()).alternateLink.replace(/\/edit.+/, "/pub");
Logger.log(url);
Pattern 2:
When you have the file ID of Google Docs, you can use this.
var url = Drive.Files.get(fileId).alternateLink.replace(/\/edit.+/, "/pub");
Logger.log(url);

Share a library by using google apps script

To share spreadsheets and documents it is easy to write a script:
var sheet = SpreadsheetApp.openById("xxxxxxxxxxxxxxxxxxxxxxxx");
sheet.addViewer("email#gmail.com");
A libray ia a stand alone Google Apps Script file. Is there a comparable script to share such a file?
You can achieve it using DriveApp as follows.
var file = DriveApp.getFileById("xxxxxxxxxxxxxxxxxxxxxxxx");
file.addViewer("email#gmail.com");
If you want to share files as ANYONE, you can use the following sample.
var file = DriveApp.getFileById("xxxxxxxxxxxxxxxxxxxxxxxx");
file.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.VIEW);
References :
Access
Permission

Convert Google Sheets to .xls

Would anyone have any pointers as to how to convert a Google sheets file to a .xls file through Google App Script?
Many thanks
Here's what I theorized, but the logic is flawed. I've also attempted the Blobs approach as well and it seems most files are defaulted to application/pdf.
function gs2xls() {
var gSheets = DocsList.getFilesByType('spreadsheet');
for(var i = 0; i < gSheets.length; i++){
var gSheetFile = gSheets[i]; //File
var gSheetSS = SpreadsheetApp.open(gSheetFile); //Spreadsheet of gs file
var gSheet = gSheetSS.getActiveSheet();
var xlsSS = SpreadsheetApp.create(gSheetFile.getName() + ".xls");
xlsSS.setActiveSheet(gSheet);
}
}
Google Apps Script can't export anything else than pdf natively but the document API does so you can use urlFetch with parameters to get what you want.
The code is shown with explanations in this post : Google apps script to email google spreadsheet excel version
It works pretty well.
That approach will never work because:
1) You are creating and empty google spreadsheet, not a file of type xls.
2) you are setting as active sheet an invalid sheet (from another spreadsheet whivh is not what setactivesheet does).
The most you will be able to achieve is to show the user an export link that downloads the file as xls. Google how to make the export link.

Script to unzip attachment

I was trying to create a Google Apps Script, that takes attachment of e-mails, unzips it a forwards it. I saw unpacking feature in Google Drive (you can upload a file there, open it and copy individual files to you drive). Is this functionality accessible from Google Apps Script somehow?
First you need to write a script for automatically makes attachments of an email as directly uploaded on user's Google Drive after this trigger run this piece of code shown below
function testzip(){
var files=DocsList.getRootFolder().find('Sans titre.txt.zip');
var zipblob=files[0].getBlob();
var unzipblob = Utilities.unzip(zipblob);
var unzipstr=unzipblob[0].getDataAsString();// it is a text file
DocsList.createFile('Sans titre.txt',unzipstr);// I kept the original name to make it simple
}
Check this code. Don't blame me if it won't work. It's just a proposal.
Comment: Files are blobs so need to call getBlob(). Anything that has a getBlob() function can be used as a blob directly. You can replace
var zipblob=files[0].getBlob();
var unzipblob = Utilities.unzip(zipblob);
with this:
var zipfile=files[0];
var unzipblob = Utilities.unzip(zipfile);
I tried to edit the other answer, but someone who apparently didn't try the code rejected the edit as incorrect.