My goal is to have the user open a file picker, select a .json file and then have the script read the file.
At the moment I have a picker working, but I cannot figure out how to limit it to just show me files with the .json extension.
I figured it out:
To open just a JSON file in picker, you need the below view:
new google.picker.View(google.picker.ViewId.DOCS).setMimeTypes("application/json");
to read the file you picked, you need to grab the ID of the file you picked, and then run it through the below function:
function readJson(id) {
var file =DriveApp.getFileById(id);
var blob = file.getBlob();
var asString = blob.getDataAsString();
var asJson = JSON.parse(asString);
}
Related
I am trying to upload a file to S3 using apps script.
I have been trying to use https://github.com/viuinsight/google-apps-script-for-aws
S3.init();
S3.putObject("bucket123", 'Tacofile.txt', content, 'ca-central-1')
I retrieve a file from Google Workspace, where 'Tacofile' is a .txt file
The file successfully loads to S3
The file, however, somehow gets converted to json? How to keep the file as a csv or is there a way to specify the MIME type somewhere before the upload?
thanks in advance
CB
Yes use File.getBlob() S3.putObject("bucket123", 'Tacofile.txt', content.getBlob(), 'ca-central-1')
#theWizEd thanks for pointing me in the right direction! I think I got this to work. I changed my source data so my download was done using:
object = DriveApp.getFileById(file_id).getBlob().getDataAsString();
i changed the section in line 106 below:
if (notBlob) {
object = Utilities.newBlob(JSON.stringify(object), "application/json");
object.setName(objectName);
}
var content = object.getDataAsString();
var contentType = object.getContentType();
var contentMD5 = getContentMD5(content);
to the following
if (notBlob) {
//object = Utilities.newBlob(JSON.stringify(object), "application/json");
//object.setName(objectName);
}
var content = object;
var contentType = MimeType.PLAIN_TEXT
var contentMD5 = getContentMD5(content);
I am currently trying to export a sheet as a pdf. This works fine using the getBlob() parameter, but I wish to export with a custom name of the file. When trying to use the following, the MimeType throws up an error message, and if removed the file is exported as plain text which I don't want. Any help would be great, either with exporting the file as a pdf or renaming it later
var sqc = ss.getSheetByName('SQC')
var folder = DriveApp.getFolderById('FOLDER ID')
//var pdf = folder.createFile(ss.getBlob());
var pdf = folder.createFile('New Text File', ss.getBlob, MimeType.exportPDF)
Based on my understanding you are trying to export your Google Sheet to a PDF file.
Here are some of my observations:
You are using an invalid MimeType, it should be MimeType.PDF
When you use createFile(name, content, mimeType), It expects a string input (Not a blob object). If you use a blob object as its content, the output pdf file will have the text Blob in it.
The most easiest way to export your sheet to pdf is to set your blob's name with file extension.
Sample Replication:
var blob = ss.getBlob();
// Test 1, set the file name and mimetype using createFile()
folder.createFile("Test1",blob,MimeType.PDF);
// Test 2, set the file name with extension in the blob object
blob.setName("Test2.pdf");
folder.createFile(blob);
Output:
Test1
Test2
I was able to sort it out. Just chuck in
pdf.setName = "Name"
I am looking to read a json file, namely config.json from a AppScript to generate a Google Form from the meta data in the config.json. I am not sure how to read the JSON file - could someone help me with it please? Some example code would be greatly appreciated.. Thanks
Assuming that your file is in Google Drive, you can use the DriveApp service to read the file and parse it.
function getFileContent() {
var fileName = "config.json.txt";
var files = DriveApp.getFilesByName(fileName);
if (files.hasNext()) {
var file = files.next();
var content = file.getBlob().getDataAsString();
var json = JSON.parse(content);
Logger.log(json);
}
}
Make sure that Google Drive has not converted the text file into a native format else the script won't be able to parse it.
If you're like me, you may have looked here to see how to store the JSON file inside the actual Apps Script project (e.g. alongside Code.gs) and then retrieve it in Code.gs.
I did this:
Add an HTML file to your project.
Remove all the content of the HTML file and put in your JSON
In your Code.gs file, use something like the following:
//Assume the file you added to your project is called 'my-json.html'
const jsonString = HtmlService.createHtmlOutputFromFile("my-json.html").getContent();
const jsonObject = JSON.parse(jsonString);
Starting with 'google apps script'. I've created a simple script that logs some information with 'Logger.log("message");' with this lines being in a loop, so the log message is quite large.
I'd like now to create an html file with that same information and either being able to download that file locally or have it downloaded to my google drive.
Any idea how that could be achieved? I've tried looking examples for a while but I cannot have anything working...
Many thanks!
Yes you can, you should read first. Have a look this documentation
function createMyHTMLlog()
{
var folders = DriveApp.getFoldersByName('ff');// ff:your folder name on drive
if (folders.hasNext())
{
var folder = folders.next(); //select the folder
folder.createFile('Log','<b> your message here </b>', MimeType.HTML); //create the file
}
}
Update: Add more content later
function logUpdate()
{
var children = folder.getFilesByName('Log');// log is the created file name
if (children.hasNext())
{
var file = children.next();
var logContent = file.getAs('text/plain').getDataAsString();// get available msg
logContent = logContent.concat('<br>your<b> updated message </b> here'); // combine with new msg
file.setContent(logContent);
}
}
Hi I'm trying to download multiple files from a google folder as txt. I'm using the google script. So far my code will successfully download the first file from a folder but it stops and wont download the rest of the files in that folder. Thanks
function doGet() {
var folder = DriveApp.getFolderById('FolderKeyHere')
var files = folder.getFiles();
while (files.hasNext()) {
var file = files.next();
Logger.log(file.getName());
var id = file.getId();
var doc = DocumentApp.openById(id);
var string = doc.getBody().getText();
var output = ContentService.createTextOutput();
output.setMimeType(ContentService.MimeType.TEXT);
output.setContent(string);
output.downloadAsFile(file.getName());
return output;
};
};
This won't be possible like that, the return output; will actually "return" the app and of course exit the while loop.
I think you won't be able to loop this kind of process without user action.
You might build a small Ui that would have a button to download files one by one on each button click but I guess that's not what you are looking for.
Why not create all the files in your drive, compress all the files in a single zip file, download the zip and then delete the files and the zip from your drive ?
Slightly longer to write but -I think - the only useable workflow to get multiple files in a single download.