Read a CSV from GitHub to Google sheets - csv

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

Related

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

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.

Bulk copy files on google drive from a list of hyperlinks

I have a list of 200 hyperlinks saved on a spreadsheet. Those links are for files (particularly Google Slides files) all saved in Google Drive. They are scattered in sub folders under the same root folder that has ~1500 files
Link 1
Link 2
Link 3
...
Link 200
I want to make a copy of those 200 files only. There is no common search term or filter to pull them up on Google Drive search. So I need to work off that list
Thoughts on doing this? Thanks in advance!
I believe your current situation and your goal as follows.
You have the Spreadsheet including 200 hyperlinks of Google Slides like https://docs.google.com/presentation/d/FILE_ID.
You want to copy the Google Slides to the specific folder in your Google Drive.
You want to achieve this using Google Apps Script.
From the number of hyperlinks, I thought that in this case, the batch request might be useful for your situation. When the batch request is used, the process cost will become low because the batch request is run with the asynchronous process. So, in this answer, I would like to propose to copy the files of 200 hyperlinks using the batch request. The sample script is as follows.
Usage:
1. Install a Google Apps Script library.
In this script, in order to achieve the batch request, a Google Apps Script library is used. Ref I thought that the request of the batch request might be a bit complecate. Ref So I created this library for using the batch request with Google Apps Script. The library's project key is as follows.
1HLv6tWz0oXFOJHerBTP8HsNmhpRqssijJatC92bv9Ym6HSN69_UuzcDk
The method for installing the library can be seen at the official document. Ref
2. Sample script.
Please copy and paste the following script to the script editor of your Google Spreadsheet including 200 hyperlinks like https://docs.google.com/presentation/d/FILE_ID. This script used Drive API. So, before you use this script, please enable Drive API at Advanced Google services. And, run the function "myFunction".
function myFunction() {
const sheetName = "Sheet1"; // Please set the sheet name.
const destinationFolderId = "###"; // Please set the destination folder ID.
// 1. Retrieve file IDs from Spreadsheet.
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
const fileIds = sheet.getRange("A1:A" + sheet.getLastRow()).getValues().reduce((ar, [a]) => {
if ((/^https:\/\/docs.google.com\/presentation\/d/).test(a)) ar.push(a.split("/")[5]);
return ar;
}, []);
console.log(fileIds) // You can check the retrieved file ID.
// 2. Retrieve the filenames form the file IDs using the "Files: get" method with the batch request.
const requests1 = fileIds.map(id => ({
method: "GET",
endpoint: `https://www.googleapis.com/drive/v3/files/${id}?supportsAllDrives=true`,
}));
const res1 = BatchRequest.EDo({batchPath: "batch/drive/v3", requests: requests1});
console.log(res1) // You can check the retrieved file metadata.
// 3. Copy the files using the file IDs and filenames using the "Files: copy" method with the batch request.
const requests2 = res1 .map(({id, name}) => ({
method: "POST",
endpoint: `https://www.googleapis.com/drive/v3/files/${id}/copy?supportsAllDrives=true`,
requestBody: {parents: [destinationFolderId || "root"], name},
}));
const res2 = BatchRequest.EDo({batchPath: "batch/drive/v3", requests: requests2});
console.log(res2);
}
Note:
In this sample script, it supposes that the 200 hyperlinks are put in the column "A" of "Sheet1". So, please modify this for your actual situation. Please be careful this.
References:
Batch request of official document
Files: get
Files: copy
BatchRequest of Google Apps Script library
Assuming the links look something similar to this and they're stored in the A column:
https://docs.google.com/presentation/d/SLIDE_ID/edit
You can easily extract the slideId from the hyperlink which corresponds to the fileId by making use of this formula (by dragging it down the whole A column):
=REGEXEXTRACT(A1,"[-\w]{25,}")
Finally, in order to copy each file, you can make use of Apps Script’s DriveApp, something similar to this:
DriveApp.getFileById(“fileId”).makeCopy(“destination”);
However, since the fileId corresponds to a range in the sheet, you can pass directly the range - so instead of using “fileId”, you could use this:
let sheet = SpreadsheetApp.openById(“spreadsheetId”).getSheetByName(“sheetName”);
let fileId = sheet.getRange(1,2).getValue();
The snippet above is retrieving the sheet where the links are stored and then by making use of the getRange and getValue methods it retrieves the value from the B1 cell (assuming that the ids of the files will be in the B column after REGEXEXTRACT).
Note
Please bear in mind that you can extract the fileId as well directly in your script, depending on the workaround and programming language .
Reference
Files Class Apps Script;
Spreadsheet Class Apps Script;
Range Class Apps Script;
REGEXEXTRACT function.

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)

Opening CSV file into Google Sheets has certain lines being merged into a single cell

I have a webscraper creating an index of links and attributes that is outputting to csv, with the intent of opening it up in Google Sheets and using Filter Views to sort and search through it.
However when I open the csv in Sheets, groups of lines are being stuck into a single cell .
I have shared a test file that demonstrates my problem and gives me the result in the image here
I also tried opening it with LibreOffice and got the same result.
Any help would be great.
You simply have an error in the CSV. If you simply add the right double quotes (as you can see here), you will have no problem in any spreadsheet.
When I saw your CSV data, I noticed that at 6th row which has A Single Phone Call, " is not closed. I thought that this might be the reason of your issue. In this case, when the CSV data is retrieved using IMPORTDATA, I confirmed the same issue with your situation.
But, I confirmed that when Utilities.parseCsv() of Google Apps Script is used, your CSV data could be correctly parsed. So how about the following sample script?
Sample script 1:
In this sample, the Google Apps Script is used as the custom function. In this case, please use the file ID of the CSV file. Please copy and paste the following script to the script editor of Google Spreadsheet, and put the custom function of =SAMPLE("1WB2PZ1vyuarBSzjETkbWmn744ZC41Ps8") to a cell. 1WB2PZ1vyuarBSzjETkbWmn744ZC41Ps8 is the file ID of your sample CSV file.
const SAMPLE = fileId => Utilities
.parseCsv(
UrlFetchApp.fetch(`https://drive.google.com/uc?export=download&id=${fileId}`).getContentText()
);
If you want to directly retrieve the CSV data from an URL, you can also the following script.
const SAMPLE = url => Utilities.parseCsv(UrlFetchApp.fetch(url).getContentText());
Result:
When this script is used, the following result is obtained.
Sample script 2:
In this sample, the Google Apps Script is run with the script editor. Please copy and paste the following script to the script editor of Google Spreadsheet and set the variables, and then, please run myFunction. By this, the CSV data is parsed and put to the Spreadsheet.
function myFunction() {
const sheetName = "Sheet1"; // Please set the sheet name.
const id = "1WB2PZ1vyuarBSzjETkbWmn744ZC41Ps8"; // This is the file ID of your sample CSV file.
// 1. Retrieve CSV data.
const csv = DriveApp.getFileById(id).getBlob().getDataAsString();
// 2. Parse CSV data to an array.
const ar = Utilities.parseCsv(csv);
// 3. Put the array to the Spreadsheet.
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
sheet.getRange(1, 1, ar.length, ar[0].length).setValues(ar);
}
This sample script is the same result with above one.
References:
Custom Functions in Google Sheets
parseCsv(csv)

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.