Upload a textfile using script in Google spreadsheets and write the contents of the textfile into a sheet in the spreadsheet - upload

Using a custom function that is called by clicking a button on Google Spreadsheets, I want to upload a text file into a specific folder and then put the contents into a specific sheet of the spreadsheet?
I know how to create a button and call a custom function on clicking it. Now through my custom function I want to:
Ask the user to browse to the text file location, and then select the file and upload it.
Upload it to a specific location and also change the file name to a specific name I want.
Read the contents of the text file and put them into a sheet in the spreadsheet.
Thanks!

Creating a button, and assigning a macro (function) to that button, or you could just as easily create a new menu item in your Google spreadsheet. The new menu item would be achieved programmatically using a custom function, and executed using the onOpen trigger for your spreadsheet.
function onOpen(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [ {name: "Text File Upload", functionName: "text_file_upload"},
{name: "Change Name", functionName: "change_name"},
{name: "TextFile to S.sheet", functionName: "t_file_ssheet"} ];
ss.addMenu("Text Files", menuEntries);
} //this onOpen function will add a new menu to your spreadsheet called "Text Files" with 3 submenu items you can use to work with your text files.
function text_file_upload() {
Browser.msgBox("You are about to upload new text file");
//insert code here to get textfile location/Url and Textfile Data, this can be stored in array
}
function change_name() {
var newName = Browser.inputBox("Enter the new name for the text file:");
//insert code here to assign new file name to the text file
}
function t_file_ssheet(){
//insert code here to copy data from your textfile to a sheet in your spreadsheet
}
There is a very nice tutorial here Tutorial: Interacting With Your Docs List that covers most of the coding your looking for.

Related

When I save pdf using jsPDF, can I save only 3 sheets from the beginning?

First of all, I am not good at English. I am sorry
When I save the pdf file as an attachment using jspdf, can I save only 3 sheets from the beginning?
I can't find an example of saving only a specific page, so I'm asking you a question
My guess is that when you press the registration button, you can save it as 3 sheets only if the extension of the attachment is pdf
My guess ->
Register after user adds attachment form submit
If the storage extension is pdf in the saved api,
2-1. Read jsPDF and bring only the first 3 pages to make a new pdf
2-2. Save only 3 pages
function fnInsert(){
if($("#title").val() === ''){
alert("Please enter a title.");
return false;
}
var ext = $('[name=upload_0]').val().split('.').pop().toLowerCase();
if($.inArray(ext, ['pdf']) == -1) {
/* When pdf, read jsPDf and bring only the first 3 pages to create a new pdf */
}
/* Save to 3-page pdf DB */
$("#aform").attr({action:"/cms/data/insertDataMgt.do", method:'post'}).submit();
}
I don't know what to do with the annotated part

How to conditionally copy a drop down in google-sheet?

I have a drop down with a few options.
I want to conditionally copy the drop down to the following line. Keeping the selected value and its options.
I have tried:
=IF(Placements!$B3="", "", D$2)
but only the content was copied.
It is possible to copy an entire dropdown list into another cell using Apps Script.
Please see sample code below for fetching dropdown list as well as creating a dropdown with the fetched values from the original dropdown.
function myFunction() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var range = sheet.getRange(1,1).getDataValidation().getCriteriaValues();
var range2 = sheet.getRange(1,2);
var rule = SpreadsheetApp.newDataValidation().requireValueInList(range[0]).build();
range2.setDataValidation(rule)
}
With this, you can add the condition that you want before creating the dropdown copy.

Is it possible to display a Google sheet as HTML without revealing the URL of the sheet?

I know it's possible to display a Google Sheet as HTML using the spreadsheet ID and GID, i.e.:
https://docs.google.com/spreadsheets/d/SPREADSHEET_ID/gviz/tq?tqx=out:html&tq&gid=GID
But in order to view that, you have to know the URL of the sheet. What I'm wondering is whether it's possible to display this HTML version of the sheet but without revealing the URL of the sheet?
Manually
To retrieve a Spreadsheet in HTML format you can export it accordingly by clicking on File -> Download -> Web Page. That will get you a zip file with the HTML of your Spreadsheet that you can rename as you like without revealing the Spreadsheet ID.
With Apps Script
You can also automate this process creating an Apps Script function that for instance gets triggered every time you click on an inserted button (to do this you can simply click on the menu bar Insert -> Drawing and the on the three dots of the top right of this button click on assign script and set it to the name of your function).
The following function will display a modal dialog on the Spreadsheet when run that if the user clicks on Download it will automatically download the zip file with the Spreadsheet in HMTL format. This function has self explanatory comments :
function downloadHTML() {
// Get ID of the Spreadsheet
var id = SpreadsheetApp.getActive().getId();
// Get the download URL of this Spreadshet in format HTML on the background
var url = "https://docs.google.com/a/mydomain.org/spreadsheets/d/" + id + "/export?exportFormat=zip&access_token=" + ScriptApp.getOAuthToken();
// Download when clicked on the button of the opened dialog
var html = '<input type="button" value="Download" onClick="location.href=\'' + url + '\'" >';
// create an HTML output from the given string
var dialog = HtmlService.createHtmlOutput(html);
// Show a modal dialog on the Spreadsheet UI when the function is run with the Title download
SpreadsheetApp.getUi().showModalDialog(dialog, "Download");
}
Reference
createHTMLOutput
showModalDialog

Is it possible to display text in one cell based on text color from another cell?

So it's sort if reverse conditional formatting I guess?
I'm making a my own spreadsheet to consolidate all my tasks which are listed on our group's tasks spreadsheet. So far, my spreadsheet is working as I intended, except for the status column.
Basically, I want it so when I turn the task green (change font color) on the group's tasks spreadsheet, the cell beside that task on MY spreadsheet will display "Done".
You can create functions using apps script and run it using Simple Triggers. To create apps script, Go to Tools->Script Editor
Simple Triggers
Triggers let Apps Script run a function automatically when a certain event, like opening a document, occurs. Simple triggers are a set of reserved functions built into Apps Script, like the function onOpen(e), which executes when a user opens a Google Docs, Sheets, Slides, or Forms file. Installable triggers offer more capabilities than simple triggers but must be activated before use. For both types of triggers, Apps Script passes the triggered function an event object that contains information about the context in which the event occurred.
Here is an example on how to modify Google Sheets using Apps Script.
Sample Function:
/**
* The event handler triggered when editing the spreadsheet.
* #param {Event} e The onEdit event.
*/
function onChange(e){
//Select the active sheet
var activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
//Select the active cell
var activeCell = activeSheet.getActiveCell();
//Note: I want to input the status next to the task's column
var taskrow = activeCell.getRow();
var statusCol = activeCell.getColumn() + 1;
//Check if font color is green then set the status column to Done
if(activeCell.getFontColor() == '#00ff00'){
activeSheet.getRange(taskrow,statusCol).setValue('Done');
}
//Check if font color is red then set the status column to Delayed
else if(activeCell.getFontColor() == '#ff0000'){
activeSheet.getRange(taskrow,statusCol).setValue('Delayed');
}
};
onChange() method can be triggered whenever there are changes done in the sheet's user interface (like font color).
Using the getFontColor() of the Range Class in SpreadsheetApp, you can get font color of the cell in CSS notation (such as '#ffffff' or 'white').
Then you can set the cell value using setValue() of the Range Class in SpreadsheetApp
To learn more about SpreadsheetApp and its classes, you can refer to this reference:
https://developers.google.com/apps-script/reference/spreadsheet
To learn more regarding Simple Triggers and Event object:
https://developers.google.com/apps-script/guides/triggers
https://developers.google.com/apps-script/guides/triggers/events
To automate the execution of onChange(), you need to add it to your project's triggers
Open current project's triggers using the clock icon beside the play button
Click Add trigger at the bottom right page of your current project's triggers
Populate the necessary information and make sure to select "On change" under Select event type option.
Sample Output:

Google Script: getActiveSheet() when there is no Active Sheet?

I've been working on this for a couple on days, and I can't find any answer to this issue.
I don't have a programmer background, so it might be an easy question for some of you.
The issue:
I have 5 Google Forms that fill 5 different Sheets in 1 Master Google Sheet.
--> When Google Form 1 is filled, there is a new line in sheet1
--> When Google Form 2 is filled, there is a new line in sheet2
--> When Google Form 3 is filled, there is a new line in sheet3
--> When Google Form 4 is filled, there is a new line in sheet4
--> When Google Form 5 is filled, there is a new line in sheet5
Now, I would need to be able to run some script (different ones) when a new line is added to each sheets.
The idea is:
--> When a new line is added to sheet1, do this.
--> When a new line is added to sheet2, do this.
.
.
.
I tried something like this (see below). But it doesn't return anything since there is no "Activesheet" when the google form is filled and add a new row by himself to Google sheet.
function onChange(event) {
var s = event.source.getActiveSheet();
var sName = s.getName();
Browser.msgBox(sName);
}
Question:
In other words, do you have a idea about how I can get the sheet that was edited by the google form, so that I can run specific script for each sheet when it gets modified ?
the onChange trigger does not fire on form submission, use the FormSubmit trigger instead
The related event objects are specified here.
The event object source is not available, but instead you can use the event object range in combination with getSheet().
Sample:
function onChange(event) {
var s = event.range.getSheet();
var sName = s.getName();
Browser.msgBox(sName);
}

Resources