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

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)

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

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.

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)

How to import historical data (CSV) from Yahoo Finance in SpreadSheets

I try to import historical data (CSV) for APPLE. I use ImportData function in Google Sheet with
https://query1.finance.yahoo.com/v7/finance/download/AAPL?period1=1577982443&period2=1609604843&interval=1d&events=history&includeAdjustedClose=true
but the result is "#N/A".
I want to get the CSV because there is 3 decimals. And only 2, on the website.
=IMPORTXML("https://query1.finance.yahoo.com/v7/finance/download/AAPL?period1=1577982443&period2=1609604843&interval=1d&events=history&includeAdjustedClose=true")
There is a script after to obtain the file : AAPL.csv .
Can you help me ?
Unfortunately, in the case of the URL, it seems that IMPORTDATA and IMPORTXML cannot be used. But, fortunately, I confirmed that UrlFetchApp of Google Apps Script can be retrieved the CSV data. So, in this answer, I would like to propose to use Google Apps Script for achieving your goal.
Sample script:
Please copy and paste the following script to the script editor of Google Spreadsheet, and please put =SAMPLE("https://query1.finance.yahoo.com/v7/finance/download/AAPL?period1=1577982443&period2=1609604843&interval=1d&events=history&includeAdjustedClose=true") to a cell. This script is used as the custom function. By this, the CSV data can be retrieved in the cells.
const SAMPLE = url => Utilities.parseCsv(UrlFetchApp.fetch(url).getContentText());
Result:
When above script is used, the following result is obtained.
References:
Custom Functions in Google Sheets
Class UrlFetchApp
Utilities.parseCsv()

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.