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.
Related
I need using the link to download the Google Sheet to .xlxs format.
This is works for me. but it is for single tab only, the thing is I have to download three or more tabs. I believe the format of "gid" would be different.
https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=1eVcHMWyH1YIDN_i0iMcv468c4_jnPk9Tw5gea-2FCyk&gid=626501804&exportFormat=xlsx
I believe your goal is as follows.
You want to export a Google Spreadsheet in XLSX format.
You want to include several specific Sheets in the Google Spreadsheet.
In this case, how about the following workaround? In this workaround, a Google Spreadsheet including the several sheets you want to include is created as a temporal Spreadsheet. In this case, as a simple method, Google Apps Script is used for retrieving the URL.
Sample script:
function sample() {
const exportSheetNames = ["Sheet1", "Sheet2", "Sheet3"]; // Please set the sheet names you want to export.
const spreadsheetId = "###"; // Please set your Spreadsheet ID.
const source = SpreadsheetApp.openById(spreadsheetId);
const temp = source.copy("temp_" + source.getName());
temp.getSheets().forEach(s => {
if (!exportSheetNames.includes(s.getSheetName())) temp.deleteSheet(s);
});
const url = `https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=${temp.getId()}&exportFormat=xlsx`;
console.log(url);
}
When this script is run, you can see the URL for exporting the Spreadsheet in XLSX format including the specific sheets you want at the log. From your question, I thought that you might want the URL for exporting.
This is a simple sample script for achieving your goal. For example, if you want to automatically export the XLSX file using a script, you can see the sample script at this thread.
I'm looking for a script that I can run from Google Spreadsheet that will open a specific Excel file that is located on my computer.
Is it possible?
Thanks!
Not totally sure what you mean with running from Google Spreadsheet, if you want to execute a script by clicking a button in a specific Google Spreadsheet for example, that’s possible. See Google’s documentation: https://developers.google.com/apps-script/guides/menus.
As far as I know access local files is impossible, since GAS is a cloud based service so it won’t be able to connect to your personal computer. My solution would be to add the specific Excel file(s) to your Google Drive. When those file(s) are located in your Google Drive folder any Google App Script can access them.
EDIT
You can open a Excel file with Google Spreadsheet(only if the file is located in your drive). With a right-mouse-click you can choose open with > Google Spreadsheets, this creates an duplicate of your Excel file as Google Spreadsheet. If opened copy the URL, if written a simple script you can start with, which works fine for me:
function myFunction() {
var ss = SpreadsheetApp.openByUrl("url");
var sheets = ss.getSheets();
for(var i = 0; i < sheets.length; i++){
var range = sheets[i].getRange(1, 1, sheets[i].getLastRow(), sheets[i].getLastColumn());
var values = range.getValues();
for(var j = 0; j < sheets[i].getLastRow(); j++){
Logger.log(values[j][0].toString());
}
}
}
This code loops through all sheets in your Spreadsheet(outer for-loop) and get the range of each sheet, it convert this range to values and logs every value in the first column from every row(inner for-loop).
You can attach this script to a button in a Spreadsheet as explained in the Google documentation. Hope this will answer your question, good luck!
I try open a xls with app script.
I can get the content type of file with:
var attach = messages[j].getAttachments()[0];
var name = attach.getName();
var type = attach.getContentType();
And I get:
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
but, How I can read the information in the cells ?
EDIT
If I use
DocsList.createFile(attach);
the attach is added in google Docs list.
I solved uploaded the file with drive
https://gist.github.com/4666836
I have a Google SpreadSheet (filled through a form) associated to a Google Apps Script.
Now, the spreadsheet contains too many cells and I'm not able to load it anymore (server timeout).
I'm not interested in the data, but I really want to retrieve the script (the source code).
Unfortunately, the only way I know is to load the SpreadSheet first :-(
(Copying the Document ends in a timeout and the script is not copied.)
Any suggestion?
Regards,
Rémi.
Create another script which makes a copy of original spreadsheet to backup the data and then clear the original spreadsheet through the script.
However this seems an issue which should be reported to Google Spreadsheet Forum
Here is an example code which will backup the spreadsheet as a copy and clear the original spreadsheet
function backupDataAndClear(){
var sourceSS = SpreadsheetApp.openById('ID_OF_ORIGINAL_SS');
var newSS = sourceSS.copy('Backup SS');
var sourceSheets = sourceSS.getSheets();
for(var i in sourceSheets){
sourceSheets[i].clear();
}
}
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.