Load or Read a JSON from local in Google AppScript - google-apps-script

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);

Related

Why does uploading a .csv file to S3 get converted to a json

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);

Trying to export to pdf using google app scripts

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"

PDF Realtime down load and conversion

Im Looking for a way to use Google Apps Script to Download PDF file and convert file into Google Sheets.
The reason for this is that website only gives data in PDF form and i cant use Import function to get data for real-time updates
It depends on the way you will download your pdf files.
Here is simple example how you can convert PDF file from your Google Drive into Googe Document:
function pdfToDoc() {
var fileBlob = DriveApp.getFilesByName('test.pdf').next().getBlob();
var resource = {
title: fileBlob.getName(),
mimeType: fileBlob.getContentType()
};
var options = {
ocr: true
};
var docFile = Drive.Files.insert(resource, fileBlob, options);
Logger.log(docFile.alternateLink);
}
To make it work you need to enable Drive API:
Based on the answer: https://webapps.stackexchange.com/a/136948
And as far as I can see there is only DOC as output. Probably you can extract data from DOC and put it into Spreadsheet with script. But it depends on how exactly looks your data.

Google apps script: How to create a downloadable html file?

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);
}
}

How can I specify an extension when using the Picker (specifically .json)

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);
}