Importing multiple CSV files to google sheets with Google Script - google-apps-script

I am working on a google script to import CSV files from web urls to individual sheets in a google sheet. I would like to have it clear the contents of certain columns before importing the new info. I had had trouble getting the script to clear contents prior to importing. I have also had in include part of one CSV along with another on a sheet. I think I need to add something to the script between CSV files. I also need something added to clear contents prior to importing. I don't want to just clear the sheet because there are formulas that need to remain. I also don't want to use the Google IMPORTDATA function because it is unreliable.
Here is my current script (URLs removed):
function importCSVFromWeb() {
var csvUrl = "http://csvurlhere";
var csvContent = UrlFetchApp.fetch(csvUrl).getContentText();
var csvData = Utilities.parseCsv(csvContent);
var ss = SpreadsheetApp.openByUrl('spreadsheeturlhere');
var sheet = SpreadsheetApp.getActiveSheet();
var ss = sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
var csvUrl = "http://csvurlhere";
var csvContent = UrlFetchApp.fetch(csvUrl).getContentText();
var csvData = Utilities.parseCsv(csvContent);
var sheet = SpreadsheetApp.getActive().getSheetByName('RentPaid');
var ss = sheet.getRange(3, 3, csvData.length, csvData[0].length).setValues(csvData);
var csvUrl = "http://csvurlhere";
var csvContent = UrlFetchApp.fetch(csvUrl).getContentText();
var csvData = Utilities.parseCsv(csvContent);
var sheet = SpreadsheetApp.getActive().getSheetByName('Tenants');
var ss = sheet.getRange(1, 2, csvData.length, csvData[0].length).setValues(csvData);
var csvUrl = "http://csvurlhere";
var csvContent = UrlFetchApp.fetch(csvUrl).getContentText();
var csvData = Utilities.parseCsv(csvContent);
var sheet = SpreadsheetApp.getActive().getSheetByName('Owners');
var ss = sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
}

To clear the contents of a sheet or a range of cells within a sheet, you can use sheet.clearContents() or range.clearContent(), which will remove only values & leave your formatting & formulas in tact.
I note that your line var ss = SpreadsheetApp.openByUrl('spreadsheeturlhere'); does nothing as you subsequently redeclare the ss variable. If the script is bound to a spreadsheet, you don't need this line as calls to getActive() & getActiveSheet() will give you a reference to the host spreadsheet. Your calls to SpreadsheetApp.getActive().getSheetByName() are returning references to the host spreadsheet anyway, not the one you open by URL.
I would also consider changing your function to accept the CSV source URLs, sheet names & cell anchors as parameters so that the code is easier to maintain. e.g.
function importCSVFromWeb(csvUrl, sheetName, dataAnchor) {
/* csvUrl: the CSV source URL
sheetName: the name of the sheet to add the data
dataAnchor: the cell origin for the range.setValues() call as an array
i.e. dataAnchor = [row, col]
*/
var csvContent = UrlFetchApp.fetch(csvUrl).getContentText();
var csvData = Utilities.parseCsv(csvContent);
var ss = SpreadsheetApp.getActive();
// note that you'll need to pass in a name for each sheet,
// including the first one where you used SpreadsheetApp().getActiveSheet() previously
var sheet = ss.getSheetByName(sheetName);
sheet.getRange(dataAnchor[0], dataAnchor[1], csvData.length, csvData[0].length).setValues(csvData);
}
function feedCSVImport(){
var urls_sheets_list = [["csv_source_url_1", "Sheet1", [1,1]],
["csv_source_url_2", "RentPaid", [3,3]],
["csv_source_url_3", "Tenants", [1,2]],
["csv_source_url_4", "Owners", [1,1]]
];
for(var i = 0, lim = url_sheets_list.length; i < lim; ++i){
importCSVFromWeb(url_sheets_list[i][0], url_sheets_list[i][1], url_sheets_list[i][2]);
}
}

Related

How do i paste into another file through app script

I am using this code:
function copyInfo() {  
var ss = SpreadsheetApp.getActiveSpreadsheet();  
var copySheet = ss.getSheetByName("Product-analyse");  
var pasteSheet = ss.getSheetByName("Blad18"); // get source range  
var source = copySheet.getRange(93,7,39,6);  // get destination range  
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,39,6); // copy values to destination range  
source.copyTo(destination);  // clear source values
source.clearContent();
} 
Now its pasting in "Blad18". Instead I want it to be pasted in another file.
I tried adding {contentOnly: TRUE}. Though this didnt seem to work
I cannot figure this out unfortunately. Everything else is correctly set already.
Any help is greatly appreciated.
Updated script I am using:
function copyInfo() {
var ssSource = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ssSource.getSheetByName("Product-analyse");
var ssPaste = SpreadsheetApp.openById("1qhnA8L3ZkU-iEyOJtszAC4OW58sg2TJZSa_rmN_EXBI"); // the id of the other spreadsheet
var pasteSheet = ssPaste.getSheetByName("Submitted Data"); // get source range
var source = copySheet.getRange(93,7,39,6); // get destination range
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,39,6); // copy values to destination range
source.copyTo(destination); // clear source values
source.clearContent();
}
Try this:
function copyInfo() {
var ss = SpreadsheetApp.getActive();
var csh = ss.getSheetByName("Sheet0");
var psh = ss.getSheetByName("Sheet1");
const cshsr = 2;
var crg = csh.getRange(cshsr, 1, csh.getLastRow() - cshsr + 1, csh.getLastColumn());
var prg = psh.getRange(psh.getLastRow() + 1, 1);
crg.copyTo(prg);
}
I believe what you want to do is copy from one spreadsheet file to another spreadsheet file. Here is an example
function copyInfo() {
var ssSource = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ssSource.getSheetByName("Product-analyse");
var ssPaste = SpreadsheetApp.openById("dfsfhsfhskfhaskjfhsjkfas"); // the id of the other spreadsheet
var pasteSheet = ssPaste.getSheetByName("Blad18"); // get source range
var source = copySheet.getRange(93,7,39,6); // get destination range
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,39,6); // copy values to destination range
source.copyTo(destination); // clear source values
source.clearContent();
}
Reference
SpreadsheetApp.openById()

comma error while importing csv using appscript to google sheets

I am trying to import a csv file to google sheets via google appscript. everything works well except in the last few rows the data is being copied after skipping a row and also it keeps adding more commas to the those empty rows. not sure why. i Have added a test file for reference
https://docs.google.com/spreadsheets/d/1jnzPalCAgO84kxUBrM-aIobIDHu7c99VStJySEyUkKo/edit?usp=sharing
function getCSVAndAppend(spreadsheetId, folderId, filename) {
var folder = DriveApp.getFolderById('1TMFXWDTJpwqTY0JsCefuAean4n9fWIIh');
var files = folder.getFilesByName('Dealers joined data.csv');
var openSpreadsheet = SpreadsheetApp.openById('1E5z7Qd3KRb5geNKlQiYhliYCOtJ0A8ICmZFcUcA1-lI');
var activeSheet = SpreadsheetApp.getActiveSheet();
if (files.hasNext()) {
var file = files.next();
var csv = file.getBlob().getDataAsString();
var csvData = Utilities.parseCsv(csv);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var header = csvData.shift();
var lastrow = activeSheet.getLastRow();
activeSheet.getRange(lastrow + 1, 1, csvData.length, csvData[0].length).setValues(csvData);
}
}

How to show ONLY filtered data when publishing the sheet?

I am importing some data (CSV file from URL). And I am using this script to update data whenever the source gets updated :
function importCSVFromWeb() {
// Provide the full URL of the CSV file.
var csvUrl = "https://covid19.who.int/WHO-COVID-19-global-table-data.csv";
var csvContent = UrlFetchApp.fetch(csvUrl).getContentText();
var csvData = Utilities.parseCsv(csvContent);
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
}
After I filter my data in google spreadsheets and every time the source gets updated, the filtered data are not filtered anymore!. I want to publish the sheet with filtered data only
My question is: After importing and filtering the data, How can I keep my filter on the data even when the source gets updated?
I believe your current situation and your goal as follows.
You have a Spreadsheet installed the basic filter.
When you update the Spreadsheet by your script, you want to continue to use the existing basic filter.
In this case, how about reinstall the basic filter by copying the existing basic filter? When this is reflected to your script, it becomes as follows.
Modified script:
function importCSVFromWeb() {
// Provide the full URL of the CSV file.
var csvUrl = "https://covid19.who.int/WHO-COVID-19-global-table-data.csv";
var csvContent = UrlFetchApp.fetch(csvUrl).getContentText();
var csvData = Utilities.parseCsv(csvContent);
var sheet = SpreadsheetApp.getActiveSheet();
// --- I added below script.
var filter = sheet.getFilter();
var range = filter.getRange();
var criteria = [];
var start = range.getColumn();
var end = start + range.getNumColumns() - 1;
for (var i = start; i <= end; i++) {
var criterion = filter.getColumnFilterCriteria(i);
if (criterion) criteria.push({col: i, c: criterion.copy()});
}
filter.remove();
// ---
sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
// And, I added below script.
criteria.forEach(({col, c}) => range.createFilter().setColumnFilterCriteria(col, c));
}
References:
getFilter() of Class Sheet
createFilter() of Class Range

Parse CSV in Google Spreadsheets

I'm trying to automatically parse a CSV document into separate cells in Google Spreadsheets. I'm using the script below, however, it just presents the , separated value, whereas, I need those values appear in individual cells. Any ideas?
function importCSVFromWeb() {
var csvUrl = "https://ctrlq.org/data.csv";
var csvContent = UrlFetchApp.fetch(csvUrl).getContentText();
var csvData = Utilities.parseCsv(csvContent);
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
}

Append rows from other tabs without losing formatting

In an effort to organize a set of bank account and credit card statements, I download them. Then I open them in Excel or Google Sheets, reorganize them in matching columns, put all in one tab (TOTAL), and then sort.
The question is now how I do the copy from the individual sheets. I think I'm pretty close after pasting and repeating (the manual way) some function I found by searching. The problem is that this function removes all formatting from the source rows
Example sheet
The last tab example_TOTAL is put there only to show how I want tab TOTAL to look after the process is done.
Here's my function so far which does the most, but erases formatting (colors).
function Copy() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var ss = spreadsheet.getSheetByName('TabA'); //replace with source Sheet tab name
var range = ss.getRange('A1:L'); //assign the range you want to copy
var data = range.getValues();
var ts = spreadsheet.getSheetByName('TOTAL'); //replace with destination Sheet tab name
ts.getRange(ts.getLastRow()+1, 1, data.length, data[0].length).setValues(data);
var ss = spreadsheet.getSheetByName('TabB'); //replace with source Sheet tab name
var range = ss.getRange('A1:L'); //assign the range you want to copy
var data = range.getValues();
ts.getRange(ts.getLastRow()+1, 1, data.length, data[0].length).setValues(data);
var ss = spreadsheet.getSheetByName('TabC'); //replace with source Sheet tab name
var range = ss.getRange('A1:L'); //assign the range you want to copy
var data = range.getValues();
ts.getRange(ts.getLastRow()+1, 1, data.length, data[0].length).setValues(data);
var ss = spreadsheet.getSheetByName('TabD'); //replace with source Sheet tab name
var range = ss.getRange('A1:L'); //assign the range you want to copy
var data = range.getValues();
ts.getRange(ts.getLastRow()+1, 1, data.length, data[0].length).setValues(data);
}
function copyFromTo() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var ss = spreadsheet.getSheetByName('TabA');
var copy = ss.getRange('A1:L');
var target = spreadsheet.getSheetByName('TOTAL');
var dest = target.getRange('A1:L');
copy.copyTo(dest);
var ss = spreadsheet.getSheetByName('TabB');
var copy = ss.getRange('A1:L');
copy.copyTo(dest);
var ss = spreadsheet.getSheetByName('TabC');
var copy = ss.getRange('A1:L');
copy.copyTo(dest);
var ss = spreadsheet.getSheetByName('TabD');
var copy = ss.getRange('A1:L');
copy.copyTo(dest);
}
How can I append from these sheets and still keep the formatting?