Download Google Sheet to .xlsx Format using Link - multiple tabs - google-apps-script

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.

Related

Read a CSV from GitHub to Google sheets

Similar to this, I wish to read data stored in csv format in a GitHub repository directly into Google sheets.
An example of the data in question is here. When I tell google sheets to import the (raw) data from this url, it finds no matching file: .
Can a direct import be achieved this simply, or does it necessarily involve using an API?
In your situation, how about the following patterns?
Pattern 1:
In this pattern, IMPORTDATA is used. A sample formula is as follows. Please put this formula into a cell of Spreadsheet.
=IMPORTDATA("https://raw.githubusercontent.com/emagar/elecRetrns/master/data/pred1964-on.csv")
Pattern 2:
In this pattern, Google Apps Script is used. Please copy and paste the following script to the script editor of Spreadsheet and run the script. By this, the retrieved CSV data is put into the active sheet.
function myFunction() {
// Retrieve CSV data and parse it.
const url = "https://raw.githubusercontent.com/emagar/elecRetrns/master/data/pred1964-on.csv";
const str = UrlFetchApp.fetch(url).getContentText();
const ar = Utilities.parseCsv(str);
// Put the values into the active sheet.
const sheet = SpreadsheetApp.getActiveSheet(); // or SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1")
sheet.getRange(1, 1, ar.length, ar[0].length).setValues(ar);
}
References:
IMPORTDATA
fetch(url)
parseCsv(csv)
If you have to do that only once, this can be helpful -
Have you tried the following steps:
Copy and paste the data from Here to notepad
Save the notepad file as xyz.csv

How do i get google doc name from url in google sheet?

image of my sheet
This is my sheet image
I tried to search but didn't find any solution.
I believe your goal is as follows.
From your following sample spreadsheet (this image is from your question.),
You want to retrieve the URLs of Google Document files from the cells "B3:B", and you want to retrieve the document titles and want to put the document titles in the column "A".
In this case, how about the following sample script? Unfortunately, in the current stage, your goal cannot be achieved by the built-in functions. So, in this answer, Google Apps Script is used.
Sample script:
Please copy and paste the following script to the script editor of Google Spreadsheet and set your sheet name to sheetName and save the script. And, please run myFunction with the script editor. And, please authorize the scopes. By this, the script is run. When this script is run, the google document URLs are retrieved from "B3:B" and converted the URLs to the document titles, and the titles are put into the column "A".
function myFunction() {
const sheetName = "Sheet1"; // Please set your sheet name.
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
const range = sheet.getRange("B3:B" + sheet.getLastRow());
const values = range.getDisplayValues().map(([url]) => [url ? DocumentApp.openByUrl(url).getName() : null]);
range.offset(0, -1).setValues(values);
}
Note:
In this script, your provided sample Spreadsheet is used. When you change your Spreadsheet, this script might not be able to be used. Please be careful about this.
In this script, it supposes that all URLs of "B3:B" are the URLs of Google Documents. Please be careful about this.
References:
map()
openByUrl(url)
getName()

List of all tabs' "publish to web" links on a large googlesheet document (200 tabs)

Is there a way to get a list of each of the hyperlinks created by the "publish to web" function on google sheets without selecting each tab individually and copying and pasting to a spreadsheet/word document. Ideally the output being all my tab names (circa 200 of them) and the link.
Any help or advice would be greatly appreciated.
If all you wish is tab names then this is a list of tab names:
function getTabNames() {
const ss = SpreadsheetApp.getActive();
Logger.log(ss.getSheets().map(sh => sh.getName()).join(','))
}
You could use openById() if you wish.
I believe your goal is as follows.
You want to receive the Web Published URL for all sheets in a Google Spreadsheet using Google Apps Script.
You want to put the URLs to the Spreadsheet.
Issue and workaround:
When a Google Spreadsheet is published to the web, a URL like https://docs.google.com/spreadsheets/d/e/2PACX-###/pubhtml?gid=###&single=true is obtained. But, in the current stage, unfortunately, this cannot be retrieved using a script and API. Ref By this, it is required to manually create the URL.
In this answer, I would like to propose 2 patterns for achieving your goal.
Pattern 1:
In this pattern, a URL like https://docs.google.com/spreadsheets/d/e/2PACX-###/pubhtml?gid=###&single=true is used. 2PACX-### is not the Spreadsheet ID. Please be careful about this.
First, please publish to the web for your Spreadsheet, and retrieve the URL of https://docs.google.com/spreadsheets/d/e/2PACX-###/pubhtml?gid=###&single=true. In this pattern , https://docs.google.com/spreadsheets/d/e/2PACX-###/pubhtml from https://docs.google.com/spreadsheets/d/e/2PACX-###/pubhtml?gid=###&single=true is used.
Please copy and paste the following script to the script editor of Google Spreadsheet. And, please set your https://docs.google.com/spreadsheets/d/e/2PACX-###/pubhtml to baseUrl. When you use this script, please put a custom function of =SAMPLE(). By this, the URLs are returned.
function SAMPLE() {
const baseUrl = "https://docs.google.com/spreadsheets/d/e/2PACX-###/pubhtml"; // Please modify this for your URL.
return SpreadsheetApp.getActiveSpreadsheet().getSheets().map(s => `${baseUrl}?single=true&gid=${s.getSheetId()}`);
}
Pattern 2:
In this pattern, the URL like https://docs.google.com/spreadsheets/d/### fileId ###/pubhtml is used. In this case, Spreadsheet ID is used. By this, you are not required to do a hard copy of the URL.
Please copy and paste the following script to the script editor of Google Spreadsheet. When you use this script, please put a custom function of =SAMPLE(). By this, the URLs are returned.
function SAMPLE() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const baseUrl = `https://docs.google.com/spreadsheets/d/${ss.getId()}/pubhtml`;
return ss.getSheets().map(s => `${baseUrl}?single=true&gid=${s.getSheetId()}`);
}
Note:
In this case, when the sheet is not published, you cannot access the URL. Please be careful about this.
References:
map()
getSheetId()

How to fix corrupted imported online excel in google sheet?

I need this online excel:
https://members.tsetmc.com/tsev2/excel/MarketWatchPlus.aspx?d=0
in my google sheet but when I use Importdata formula as below:
Importdata("https://members.tsetmc.com/tsev2/excel/MarketWatchPlus.aspx?d=0")
it turns out to be imported in corrupted way like this:
enter image description here
but the original online file is this:
enter image description here
How Can I fix it?
I believe your goal as follows.
You want to put the values from the XLSX data downloaded from an URL to the active sheet of Google Spreadsheet.
Issue and workaround:
In the current stage, it seems that there are no method for directly retrieving the data from XLSX data of URL in the built-in functions for Google Spreadsheet. So in this answer, I would like to propose to achieve your goal using Google Apps Script. When Google Apps Script is used, your goal can be achieved.
The flow of the sample script is as follows.
Retrieve XLSX data.
Convert XLSX data to Google Spreadsheet as the temporal Spreadsheet.
Retrieve values from the converted Spreadsheet.
Put the values from the converted Spreadsheet to the active sheet.
Remove the temporal Spreadsheet.
Sample script:
Please copy and paste the following script to the script editor of the Google Spreadsheet. Before you use this script, please enable Drive API at Advanced Google services. And please run the function of myFunction at the script editpor. By this, the values of XLSX data are put to the active sheet.
function myFunction() {
// 1. Retrieve XLSX data.
const url = "https://members.tsetmc.com/tsev2/excel/MarketWatchPlus.aspx?d=0"; // This is your URL.
const blob = UrlFetchApp.fetch(url).getBlob();
// 2. Convert XLSX data to Google Spreadsheet as the temporal Spreadsheet.
const id = Drive.Files.insert({title: "temp", mimeType: MimeType.GOOGLE_SHEETS}, blob).id;
// 3. Retrieve values from the converted Spreadsheet.
const values = SpreadsheetApp.openById(id).getSheets()[0].getDataRange().getValues();
// 4. Put the values from the converted Spreadsheet to the active sheet.
const dstSheet = SpreadsheetApp.getActiveSheet();
dstSheet.getRange(1, 1, values.length, values[0].length).setValues(values);
// 5. Remove the temporal Spreadsheet.
DriveApp.getFileById(id).setTrashed(true);
}
If you want to put the values to the specific sheet and range, please modify the script of 4. Put the values from the converted Spreadsheet to the active sheet..
References:
Files: insert
getValues()
setValues(values)

Function to export only current sheet to Excel, with values and formats

I have this sample Google Spreadsheet.
I want to create a Google Apps Script function to export only the sheet named "TargetSheet" to Excel.
Since TargetSheet might have formulas, it is important that this exportation has only values. Also, the colors and formats are important to be kept.
The final file should look like this.
Someone suggested me to use this snippet to export the sheet, but since I'm a begginner in Google Apps Script I didn't quite understand how to do that. Should I copy it to my code? Should I import it somehow? And after getting this function to work in my script, I assume I would need to create a function to put that to work. Would that look like this below?
function exportarPlanilha() {
var spreadsheet = SpreadsheetApp.getActiveSheet();
var file = exportSpreadsheetToFile_(spreadsheet, 'xlsx');
return file;
}
You want to export one of sheet (TargetSheet) in the active Spreadsheet as a XLSX file.
From the sample script of URL in your question, you want to create the XLSX file in your Google Drive.
You want to achieve this using the sample script of URL in your question with Google Apps Script.
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
Flow:
When I saw the sample script of URL in your question, it seems that the flow is as follows.
Copy the active Spreadsheet as a tempora Spreadsheet.
Convert the formulas to the texts.
Delete the sheets except for a sheet you want to export.
Retrieve the export URLs.
Retrieve the blob from the export URL.
Crete the blob as a file.
Delete the temporal Spreadsheet.
In your case, the export type is the XLSX format which is the constant. So I think that the process cost can be reduced a little. So the modified script is as follows.
Sample script:
Please copy and paste the following script to the script editor of the Spreadsheet. And please set exportSheetName. Then, please run the function exportarPlanilha at the script editor. When the authorization screen is displayed, please authorize the scopes. By this, the script is run.
function exportarPlanilha() {
const exportSheetName = 'TargetSheet'; // Please set the target sheet name.
// 1. Copy the active Spreadsheet as a tempora Spreadsheet.
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet().copy('tmp');
// 2. Convert the formulas to the texts.
const targetRange = spreadsheet.getSheetByName(exportSheetName).getDataRange();
targetRange.copyTo(targetRange, {contentsOnly:true});
// 3. Delete the sheets except for a sheet you want to export.
spreadsheet.getSheets().forEach(sheet => {
if (exportSheetName != sheet.getName()) spreadsheet.deleteSheet(sheet)
});
// 4. Retrieve the blob from the export URL.
const id = spreadsheet.getId();
const xlsxBlob = UrlFetchApp.fetch(`https://docs.google.com/spreadsheets/export?id=${id}&exportFormat=xlsx`, {headers: {authorization: `Bearer ${ScriptApp.getOAuthToken()}`}}).getBlob();
// 5. Crete the blob as a file.
DriveApp.createFile(xlsxBlob.setName(`${exportSheetName}.xlsx`));
// 6. Delete the temporate Spreadsheet.
DriveApp.getFileById(id).setTrashed(true);
}
In this case, the temporal Spreadsheet is moved to the trash box.
Note:
In this case, please enable V8 at the script editor.
In above script, the XLSX file is created to the root folder. If you want to create the file to the specific folder, please tell me.
References:
copy(name)
copyTo(destination, options)
deleteSheet(sheet)
Class UrlFetchApp
createFile(blob)
setTrashed(trashed)
If I misunderstood your question and this was not the direction you want, I apologize.