Hope you can help. Im looking to create a script to upload csv into a google sheet from a folder in the drive? is there a way to auto upload this once i have added to the folder?
thanks in advance
You can use Google Apps Script to create a google sheet version of your csv file in your Google Drive. You can use the script below:
function myFunction() {
var file = DriveApp.getFilesByName(your-filename-here).next();
var csvData = Utilities.parseCsv(file.getBlob().getDataAsString());
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(1,1,csvData.length, csvData[0].length).setValues(csvData)
}
Related
I want to find and delete files in Google Drive, which contain special string in the file name.
I tried it in the following way:
created a spreadsheet named testDeleteByName, where I will run Google App script,
created a project in the Cloud Console,
activated Google Drive API for the project, created OAuth and added the spreadsheet testDeleteByName to this project in the project setting of the spreadsheet.
In the spreadsheet I added the script
function SearchFiles() {
var searchFor ='title contains "untitled"';
var files = DriveApp.searchFiles(searchFor);
while (files.hasNext()) {
files.next().setTrashed(true);
}
}
Then I created a spreadsheet named untitled testsheet to test the script.
After this I executed the script in the testDeleteByName.
As result I get
An error in execution log
Exception: Access denied: DriveApp. SearchFiles # Code.gs:5
The test file untitled testsheet is disappeared (not even in the bin)
The file testDeleteByName with the script is removed into the bin.
What I'm doing wrong? How looks the correct way?
I get a CSV data file from Google Adwords API. It is formatted like this: Keyword;"Monatliches Suchvolumen";Competition;"Suggested Bid" and has .csv file ending. Here an original data sample:
+--------------------------+-------------------------+------------------+---------------+
| Keyword | Monatliches Suchvolumen | Competition | Suggested Bid |
+--------------------------+-------------------------+------------------+---------------+
| rechtsschutzversicherung | 110000 | 0.84808008438954 | 6404121 |
+--------------------------+-------------------------+------------------+---------------+
| rechtsschutz | 12100 | 0.7662246934702 | 6050097 |
+--------------------------+-------------------------+------------------+---------------+
Then i upload it to Google Drive with File Upload. After upload the file is recognized as Google Spreadsheets file - but after opening i have all data in one column, column separation doesn't work.
If after downloading the file from AdWords API i open it in Notepad++, remove "" and replace ; with tabs, save the file and then upload it to Google Drive, column separation works and all data is in its correct column. But i can't do it manually - i have too many files.
What should i do to reach automatic column recognition in Google Spreadsheets?
Was trying: played with Google Drive settings, like Convert uploaded files to Google Docs editor format, but without success. I thought about the localization settingsdirectly in Google Spreadsheets - but this will not work, because these settings can be edited only after spreadsheets is already created - and here is all data already in one column.
I believe your current situation and goal as follows.
You have a CSV file on your Google Drive.
CSV data is as follows.
Keyword;"Monatliches Suchvolumen";Competition;"Suggested Bid"
,
,
,
You want to parse the CSV data and put to the Spreadsheet.
In this case, how about using Google Apps Script? I thought that when Google Apps Script is used, the CSV file on your Google Drive can be retrieved and parsed. The sample script is as follows.
Sample script:
Please copy and paste the following Google Apps Script to the script editor of Google Spreadsheet, and set the variable of fileId. And, when you run the function of myFunction, the authorization screen is opened. So please authorize the scopes. By this, the script retrieves the CSV data from the CSV file, and parse the CSV data and put to the active sheet.
function myFunction() {
const fileId = "###"; // Please set the file ID of the CSV file on your Google Drive.
const csv = DriveApp.getFileById(fileId).getBlob().getDataAsString();
const values = Utilities.parseCsv(csv, ";");
const sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(1, 1, values.length, values[0].length).setValues(values);
}
Note:
When you want to retrieve the CSV data from the CSV file using the filename, you can also use the following script.
function myFunction2() {
const filename = "sample.csv"; // Please set the filename of CSV file on your Google Drive.
const file = DriveApp.getFilesByName(filename);
if (!file.hasNext()) {
throw new Error(`"${filename}" was not found.`);
}
const csv = file.next().getBlob().getDataAsString();
const values = Utilities.parseCsv(csv, ";");
const sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(1, 1, values.length, values[0].length).setValues(values);
}
This is a simple sample script. So please modify this for your actual situation.
References:
getFileById(id)
parseCsv(csv, delimiter)
setValues(values)
This is my code so far. It runs but does nothing.
function Quiz1Load()
{
var ss = SpreadsheetApp.openById("1JJ6AtvvSqoX0qK6OMQgGizhwjGVxnyWmYqX_STAW0t4");
SpreadsheetApp.setActiveSpreadsheet(ss);
var sheet = ss.getSheetByName("Sheet1");
SpreadsheetApp.setActiveSheet(sheet);
}
Although you can open a file 'programmatically' with google apps script (on the server, e.g. for modification of the file through the script), you can not open it 'physically' (on the client side).
I need to be able to launch Excel and open and eiut a specific file from an MVC application.
The application is for internal use only and users will have access to the folder containing the excel file.
I have looked at the File method below
var dir = location + "Filename.xlsx";
var cd = new ContentDisposition
{
FileName = "Excel Spreadsheet",
Inline = true
};
Response.AppendHeader("Content-Disposition", cd.ToString());
return File(dir, "application/vnd.ms-excel");
However, this opens Excel with a copy of the file downloaded to the users's download directory. So if they make any changes, these do not update to the original.
Is what I'm trying to do possible? If so, how?
Many thanks,
Chris.
I would like to find out, how can I email a .txt file that is located in the application storage directory.
Thanks
Jared
Use applicationStorageDirectory and navigateToURL with mailto like so,
(Note: I am not going in depth with reading text file. I can help though if you need help. Putting it only to give some idea here.)
//Find the target file
var fileToFind:File = File.applicationStorageDirectory.resolvePath("filename.txt");
//Extract content from file
var fileStream:FileStream = new FileStream();
fileStream.openAsync(fileToFind, FileMode.READ);
//fileStream.addEventListener(Event.COMPLETE, showContent);
//fileStream.addEventListener(IOErrorEvent.IO_ERROR, alertError);
//To mail
navigateToURL(newURLRequest("mailto:someEmail#gmail.com"+"?subject=Subject"+"&body="+ textfileContents + "));
--
If you want to send attachment or HTML formatted email, see this link,
SMTPMailer
.
Best luck.