Change upload location of a Google Form to a Google Sites - google-apps-script

I need help please!
I am making a repository of proyects in Google sites. This repository is a Data base in Google Sheets feeded by a Google Form, and has an interfase in Google Sites using Awesome Tables.
My problem is that each Google Form has a file upload which the documents are automaticaly saved into my drive, but I need that this files are saved automaticaly to Google Sites so anyone can consult and change them.
Google Sites has specific buttons that can upload files, so I reckon that I can make a script saves the files directly to Googel Sites or use the link of the file in my Drive and activate this button to upload the Files.
I figured I can use this script and add the Google Sites destination, I just don't know how. Sorry for my lack of Script knowledge.
function onFormSubmit(e) {
Logger.log(JSON.stringify(e));
var ft = e.values.slice(1); //This is where are the file url were located
var fA = ft[0].split(',');
for(var i = 0; i < fA.length; i++) {
var file = DriveApp.getFileById(fA[i].slice(fA[i].indexOf("=")+1));
Drive.Files.update({"parents": [{"id": "FolderId"}]}, file.getId());
Utilities.sleep(2000);
}
}

Related

move generated google Form to specific google drive folder using google apps script

I am creating a few forms using Google Apps Script which automatically generates in my root (My Drive) of my Google Drive.
I want to move these forms to a specific folder of a Shared Drive.
I googled about moving files (or any object) to another folder, most of them are suggesting that copy the file to a specific location, then delete from the source location.
But in this case, my formIds are changing which I don't want to change.
Is there any better way to move files (in my case Forms) to another location in Google Drive?
As a developer you should always favor canonical sources over 3rd party tutorials that pop-up in google search. The official documentation for the Drive API has guides describing how to move files from one drive folder to the next. I suggest you start there.
the following code should work for moving any file, including forms, to a destination folder in Google Drive:
var fileId = '1234567890abcdefghijklmnopqrstuvwxyz'; //<--replace string with your file ID
var destinationFolderId = 'zyxwvutsrqponmlkjihgfedcba0987654321';//<--replace string with your destination folder ID
function moveFile(fileId,destinationFolderId) {
var destinationFolder = DriveApp.getFolderById(destinationFolderId);
DriveApp.getFileById(fileId).moveTo(destinationFolder);
}

Using Google Apps Scripts to generate shareable links of new files

I recently discovered this Google Apps Scripts feature and am wondering if my issue can be addressed by using that.
Let's say I have a machine generating new files (screenshots or videos taken from a capture card) that are being saved in a folder that is sync'd to my Google Drive. What I would like to do is somehow automatically generate a shareable link for each NEW file that gets added and send it to an email address (may or may not be a Gmail address).
Is this something that I can use Google Apps Scripts for? I tried looking into batch files first but I can't generate the shareable link automatically (or couldn't figure out how).
I don't have any code yet, just looking at potential approaches.
Thanks in advance!
Yes almost everything is possible in GAS. However, you are likely to find better answers when you post some code that you have tried yourself before asking the answer here.
However, if the machine automatically saves the files in a folder in google drive you can scan that folder with a script and find the links. Attach a trigger to it with a, for example, 5 minute interval and write those links on a spreadsheet.
an example:
function links(){
var sheet = SpreadsheetApp.getActive.getActiveSpreadsheet();
var folder = DriveApp.getFolderById('put google folder id here');
var files = folder.getFiles();
while (files.hasNext()) {
var file = files.next();
var vals = file.getUrl();
sheet.getRange(your range you want to paste the link).setValues(vals);
}
}
This will find the links of all the files in the folder and paste that onto a google spreadsheet where the code is linked to.
Use that spreadsheet as a database and send an email by using that database.

How To set permissions to Google Drive files/folder while adding from Google Site

I am new to Google Sites, I have created a test google site, and add some pages files from google drive to the pages.
My Question is: Is it possible to set permissions from a Google Site while adding a file/folder from Google Drive via any Google Apps Script or Google Drive SDK?
I also did some overview about Google Apps Script for adding UI elements/ html elements, and perform some basic authentication using Google Drive SDK. Given this:
Is possible to perform permission actions.
If we upload a file in a file cabinet template can we set any kind of permissions or remove download link button of that file?
Apps Script has a service called Drive. Drive has a class named: DriveApp. DriveApp can create, save and retrieve files. Once you have a reference to a file, you can use other Classes, Properties and Methods.
setSharing method - Google Documentation
Thanks to Your Answer #Sandy Good from you suggestion/idea I able to create a simple script and after creating this script I am sure I will be able to create a complicated script also.
I have created a script that will create a form in in google site's page. this form contain a dropdown list of my google drive files and an input filed in which i will add an email address.
and in the last a submit button.
after pressing submit button , the script work as it will grant permission on a selected to the email given as viewer of the file if he has link.
create a blank script in the Google Site's Apps Script Section.
Add some UI via UI Service
function doGet() {
// create ui element
var app = UiApp.createApplication();
// create form
var form = app.createFormPanel();
// create area or panel
var flow = app.createFlowPanel();
// create list box for files
var element_file = app.createListBox().setId('file_name').setName('file_name');
// add first elemnt
element_file.addItem('Please Select','');
// get drive files
var files = DriveApp.getFiles();
while (files.hasNext()) {
var file = files.next();
element_file.addItem(file.getName(),file.getId());
}
flow.add(element_file);
var element_text=app.createTextBox().setId('email').setName('email');
flow.add(element_text);
flow.add(app.createSubmitButton("Submit"));
form.add(flow);
app.add(form);
return app;
}
after this I handle the data submitted via post
function doPost(e) {
var app = UiApp.getActiveApplication();
var email = e.parameter.email;
var file_name = e.parameter.file_name;
var public_file = DriveApp.getFileById(file_name);
public_file.addViewer(email);
return app;
}
after this you can check the permissions of the specific file in the google drive.
you may also add a success message.
after this go to publish menu of the file select deploy as web app then select the required things. remember please select execute scripts only as me option because you are listing all your files.
then go to the page in which you want to add script just edit/add new page from editor of the page just insert Apps Script.
apologies , its my first time to work in google apps script , if I miss anything or i did not follow any rule etc.
Thanks,

Write a google app script to store a PDF from URL in a specific folder in google drive

So here is what I want to do:
I have a URL of a PDF document. I want to store it directly into my google drive without downloading it to my hard drive first and then uploading to google drive. Does anyone know how to do this?
Thanks in advance!!!
Using urlFetch and DocsList, this is pretty straightforward and takes only 3 lines of code :
var urlOfThePdf = 'http://www.fostexinternational.com/docs/tech_support/pdfs/280_owners_manual.pdf';// an example of online pdf file
var folderName = 'GAS';// an example of folder name
function saveInDriveFolder(){
var folder = DocsList.getFolder(folderName);// get the folde
var file = UrlFetchApp.fetch(urlOfThePdf); // get the file content as blob
folder.createFile(file);//create the file directly in the folder
}

Convert Google Sheets to .xls

Would anyone have any pointers as to how to convert a Google sheets file to a .xls file through Google App Script?
Many thanks
Here's what I theorized, but the logic is flawed. I've also attempted the Blobs approach as well and it seems most files are defaulted to application/pdf.
function gs2xls() {
var gSheets = DocsList.getFilesByType('spreadsheet');
for(var i = 0; i < gSheets.length; i++){
var gSheetFile = gSheets[i]; //File
var gSheetSS = SpreadsheetApp.open(gSheetFile); //Spreadsheet of gs file
var gSheet = gSheetSS.getActiveSheet();
var xlsSS = SpreadsheetApp.create(gSheetFile.getName() + ".xls");
xlsSS.setActiveSheet(gSheet);
}
}
Google Apps Script can't export anything else than pdf natively but the document API does so you can use urlFetch with parameters to get what you want.
The code is shown with explanations in this post : Google apps script to email google spreadsheet excel version
It works pretty well.
That approach will never work because:
1) You are creating and empty google spreadsheet, not a file of type xls.
2) you are setting as active sheet an invalid sheet (from another spreadsheet whivh is not what setactivesheet does).
The most you will be able to achieve is to show the user an export link that downloads the file as xls. Google how to make the export link.