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);
}
}
Related
I have multiple spreadsheets in a folder, for each spreadsheet and I would like to get the matching data from an external file.
For example if my spreadsheet is called 'iphone 7' I would like to get in that sheet all the records from my external file containing 'iphone 7'
Here is how far I got (I am pretty new to scripting !) :
function myfunction()
{
var root = DriveApp.getFoldersByName("Produits");
while (root.hasNext())
{
var folder = root.next(); //If the folder is available, get files in the folder
var files = folder.getFiles();
while(files.hasNext()) //For each file,
{
var spreadsheet = SpreadsheetApp.open(files.next());
//import data from URL
var csvUrl = "https://incensy.tempurl.host/test-ct-flux.csv";
var csvContent = UrlFetchApp.fetch(csvUrl).getContentText();
var csvData = Utilities.parseCsv(csvContent);
var sheets = spreadsheet.getSheets()
var sheetIndex=0
var sheet = sheets[sheetIndex]
sheet.getRange(2, 1, csvData.length, csvData[0].length).setValues(csvData);
//Only keep data that contains the file name
var name = spreadsheet.getName();
let range = sheet.getDataRange(),
maxRows = sheet.getMaxRows(),
srchCol_1 = 2,
srchPatt_1 = new RegExp(name, "i"),
newRangeVals = range.getValues().filter(r => r[0] && srchPatt_1.exec(r[srchCol_1])),
numRows = newRangeVals.length;
range.clearContent();
sheet.getRange(2,1, numRows, newRangeVals[0].length).setValues(newRangeVals);
console.log('myfunction')
sheet.deleteRows(numRows + 1, maxRows - numRows);
}
}
}
There is something wrong in the second part of the code I cannot figure out.
In your situation, how about the following modification?
Modified script:
function myfunction() {
var keywords = ["sample1", "sample2"]; // Please set the keywords you want to filter to the column "C".
// Retrieve CSV data.
var csvUrl = "https://incensy.tempurl.host/test-ct-flux.csv";
var csvContent = UrlFetchApp.fetch(csvUrl).getContentText();
var csvData = Utilities.parseCsv(csvContent, ";");
// Retrieve Spreadsheet and put the CSV data.
var root = DriveApp.getFoldersByName("Produits");
while (root.hasNext()) {
var folder = root.next();
var files = folder.getFiles();
while (files.hasNext()) {
var spreadsheet = SpreadsheetApp.open(files.next());
var name = spreadsheet.getName().toUpperCase();
var values = csvData.reduce((ar, r) => {
if (!keywords.some(e => r[2].includes(e)) && r.join("").toUpperCase().includes(name)) {
ar.push(r);
}
return ar;
}, []);
if (values.length == 0) continue;
var sheet = spreadsheet.getSheets()[0];
sheet.clearContents().getRange(1, 1, values.length, values[0].length).setValues(values);
}
}
}
In this modification, the CSV data is retrieved at the outside of the while loop. Using the retrieved CSV data and each Spreadsheet name, the data is put to the 1st tab of each Spreadsheet.
References:
reduce()
toUpperCase()
includes()
I am trying to append data from csv saved in google drive to another file in google sheets, I want to ensure that the data gets pasted without the headers. I keep getting an error.
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 csvDataNoHeader = csvData.splice(1,csvData.length-1)
var lastrow = activeSheet.getLastRow();
activeSheet.getRange(lastrow + 1, 1, csvData.length, csvData[0].length).setValues(csvData);
}
}
It's possible that there are other issues as noted by Yuri. But this splice looks incorrect to me
var csvDataNoHeader = csvData.splice(1,csvData.length-1)
I think the 1 should be zero and the other parameter should be 1 but it might actually be easier just to use shift to remove the first row. I think csvData is a 2d array.
I am trying to import data from a specific URL into all sheets that are in a Google drive folder, then only keep the data that contains the name of the file.
Getting the files from the folder seems to be working but I cannot manage to have SpreasheetApp working (I think this is where the problem comes from). Maybe there is something wrong with how I manage to get ranges.
Here is what I have done so far :
function myfunction()
{
var root = DriveApp.getFoldersByName("Produits");
while (root.hasNext())
{
var folder = root.next(); //If the folder is available, get files in the folder
var files = folder.getFiles();
while(files.hasNext()) //For each file,
{
var spreadsheet = SpreadsheetApp.open(files.next());
//import data from URL
var csvUrl = "myURL";
var csvContent = UrlFetchApp.fetch(csvUrl).getContentText();
var csvData = Utilities.parseCsv(csvContent);
var activeSpreadSheet = SpreadsheetApp.open(files);
var sheets = activeSpreadSheet.getSheets();
var sheet = sheets[sheetIndex]
sheet.getRange(2, 1, csvData.length, csvData[0].length).setValues(csvData);
// Always show 3 decimal points
var cell = sheet.getRange("D2:D");
cell.setNumberFormat("00");
//Only keep data that contains the file name
var name = sheet.getName();
let range = sheet.getDataRange(),
maxRows = sheet.getMaxRows(),
srchCol_1 = 2,
srchPatt_1 = new RegExp(name)
newRangeVals = range.getValues().filter(r => r[0] && srchPatt_1.exec(r[srchCol_1])),
numRows = newRangeVals.length;
range.clearContent();
sheet.getRange(2,1, numRows, newRangeVals[0].length).setValues(newRangeVals);
sheet.deleteRows(numRows + 1, maxRows - numRows);
//Clean headers
var cell = sheet.getRange(1,1);
cell.setValue("Marchand");
var cell = sheet.getRange(1,2);
cell.setValue("Image");
var cell = sheet.getRange(1,3);
cell.setValue("Titre de l'offre");
var cell = sheet.getRange(1,4);
cell.setValue("Prix");
var cell = sheet.getRange(1,5);
cell.setValue("Lien");
var cell = sheet.getRange(1,6);
cell.setValue("Categorie");
}
}
}
Change
var activeSpreadSheet = SpreadsheetApp.open(files);
var sheets = activeSpreadSheet.getSheets();
to (spreadsheet has already been defined previously)
var sheets = spreadsheet.getSheets()
and give a value to sheetIndex, for instance
var sheets = spreadsheet.getSheets();
var sheetIndex=0
var sheet = sheets[sheetIndex]
I have 184 audit sheets in one folder. I want to reference one cell in each of these sheets and bring them back in to one master spreadsheet.
I have a code that does the opposite that sends a value to each sheet in the folder and changes it to the value that I want. So in essence I want to do the opposite of the script below:
function getdata() {
var files = DriveApp.getFolderById("1gbA2JI1DYNku7SQPaCq1Qk27hnbimPag").getFiles()
while (files.hasNext()) {
var file = files.next();
var shoot = SpreadsheetApp.openById(file.getId());
var sourcesheet = SpreadsheetApp.getActive().getSheetByName("Jan");
var sourcerange = sourcesheet.getRange('A3');
var sourcevalues = sourcerange.getValues();
var destsheet = shoot.getSheetByName('Front Sheet');
var destrange = destsheet.getRange('B5');
destrange.setValues(sourcevalues);
}
}
Your example code doesn't appear to match your explanation but I think I got the basic idea so I made up some of the needed details on my own. I included filenames, ids and 'A3' values. You can choose to modify as needed.
function getdata(month) {
var monthA=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
var date=new Date();
var mss=SpreadsheetApp.getActive();
var ms=mss.getSheetByName("Master Sheet");
var vObj={name:[],id:[],value:[]};
var files = DriveApp.getFolderById("1gbA2JI1DYNku7SQPaCq1Qk27hnbimPag").getFilesByType(MimeType.GOOGLE_SHEETS);
while (files.hasNext()) {
var file = files.next();
var fid=file.getId();
var ss = SpreadsheetApp.openById(fid);
var sh = ss.getSheetByName(monthA[date.getMonth()]);
var rg = sh.getRange('A3');
vObj.name.push(file.getName());
vObj.id.push(fid);
vObj.value.push(rg.getValue());
vObj['month']=monthA[date.getMonth()];
}
vObj.id.splice(0,0,'ids');
ms.appendRow(vObj.id);
vObj.name.splice(0,0,vObj.month);
ms.appendRow(vObj.name);
vObj.value.splice(0,0,'values');
ms.appendRow(vObj.value)
}
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]);
}
}