Need to create dynamic hyperlink or change cell remotely - google-apps-script

I've created a google spreadsheet. It has a master sheet, and I made a query function to pull data for my team.
My team has to update the column that has a comment section. But pulled data cannot be changed.
Now I need either of solutions:
1. Change the original cell in the master sheet remotely
2. Have a dynamic hyperlink to the cell they need to update
Here what I would want for the option №2 in my google spreadsheet - excel example I found on the internet

Solution
I believe this is what you were looking for (if not please clarify a bit more your question): you want to create a dropdown list that depending on the name on it the link next to it will change accordingly to direct you to the specific cell of that person in the other sheet.
If that is the case here is the simplified piece of code that will achieve this with self explanatory comments:
function onEdit() {
// Get the two sheets
var ss1 = SpreadsheetApp.getActive().getSheetByName('Sheet1');
var ss2 = SpreadsheetApp.getActive().getSheetByName('Sheet2');
// Create an array for the dropdown list
var names = ['Ramon', 'Jose', 'Pepito'];
// Build the dropdown data validation
var dropdown = SpreadsheetApp.newDataValidation().requireValueInList(names).build();
// Set the dropdown to your desired ccell and attach the validation to that cell
var dropdownCell = ss1.getRange('A1');
dropdownCell.setDataValidation(dropdown);
// For every case of your dropdown (for every name) create an if condition so that the link on the side keeps changing
// accordingly to who is set on the drop down (i.e repeat this step for the rest of the names).
if(dropdownCell.getValue()=='Ramon'){
// Set the vale of B1 to the hyperlink formula (you need to get your linked sheet id and the range or cell you want to link it to.
ss1.getRange("B1").setValue('=hyperlink("#gid='+ss2.getSheetId()+'&range='+ss1.getRange('B112').getA1Notation()+'", "Click to jump to Sheet 2")');
}
}
For achieving this I took use of the formula hyperlink and of the Apps Script tool for creating data validation and setting it to a specific cell. Moreover, the use of onEdit() will allow you to catch instant changes in the dropdown cell.
I hope this has helped you. Let me know if you need anything else or if you did not understood something. :)

Related

How to add a new row underneath the current one on request

I am trying to build a form in Google Sheets. I have one row which is a drop down list (cell A10, data validation-list from range). The whole sheet is only columns A and B, as the others I have deleted. I need to give a way for anyone editing the file to add a copy of that cell underneath it, and keep the drop down list, so they can choose more than one option from that list if needed.
Best I can figure, this should be doable with check boxes and some scripting, but I am just now learning scripting, so I have no idea how the script should look.
I need a way to do this automatically (as in, inserting a new row with the same drop down list when they check a box, or anything else similar) as some of the people that will be filling out this form are really not technical at all, so just telling them to copy the row will not work.
Is there a way to do this?
Add Row underneath current row
function addNewRowUnderneathCurrentRow() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getActiveSheet();
sh.insertRowAfter(sh.getActiveRange().getRow())
}

How do i copy a checkbox value and keep the cell editable in Google Sheets?

I've posted this question 2 days ago but with no answers. Here's a link to a copy of the file I'm working on:
https://docs.google.com/spreadsheets/d/1TCQ0cWulb8Wm63Hs57ruwEXgpP1swKAi/edit?usp=sharing&ouid=110569351169109673921&rtpof=true&sd=true
Basically, what I've done is: the "effettivo" page is a copy of the "pianificazione" page, with all the cells in "effettivo" that copy its value from its relative. The trouble is: for the checkboxes on the right, the formula I've set (=pianificazione!G3, for example) copies the value but it renders the checkbox itself uneditable manually (= I can't click on the checkbox to change the value of that checkbox). I need to find a way to copy all those checkbox values from "pianificazione" to "effettivo" while mantaining the checkboxes themselves editable. Thanks again guys :)
First of all the document you provided is not Google sheet, but Excel sheets. To make checkboxes (or in my example whole data) two-ways editable from both sheets and show the same values in both sheets everytime. I suggest you save this document as Google sheet and use GAS onEdit() function. With this function you also able to mirror other changes in values, not only checkboxes. I built fast example based on your document here
or you can just take over the function to your document
function onEdit(e) {
var sheet = e.range.getSheet();
var value = e.value;
if (sheet.getName() == 'effettivo') {
e.source.getSheetByName('pianificazione').getRange(e.range.rowStart, e.range.columnStart).setValue(value);
}
if (sheet.getName() == 'pianificazione') {
e.source.getSheetByName('effettivo').getRange(e.range.rowStart, e.range.columnStart).setValue(value);
}
}
Important! delete all formulas, that passes values between sheets, since that will be done by function, you don't need them anyway. Or just delete old 'effettivo' sheet and duplicate the 'pianificazione' and rename it to 'effettivo'. Also very important to authorize script first, best way to do it in Script editor trigger onEdit function manually and authorize it.

Add hyperlink to a table cell within a Google Slides presentation

In my Google slideshow, I have a table. The table updates every 15 minutes. I have cells in this table that I want to include hyperlinks. I tried to manually enter a hyperlink by highlighting the text in the cell and right clicking, selecting "link". But when the table updates the hyperlinks gets removed. I've tried both setText() and insertText() in the script, both erase the links.
I've also looked into having the link automatically be re-added once the update was done, however slideshow tables don't have an insertLink command. Also, as this table is not actually a Google Spreadsheet, there is no setFormula() command.
Here is what I'm currently using to update the table. Works, just need hyperlinks added to the text.
FYI the links will be to other files in the Google Drive, if that matters...
var slide = SlidesApp.getActivePresentation().getSlides()[slideNo];
var table = slide.getTables();
var cell = table[0].getCell(row,4);
var trafficTime = convertTime(res["routes"][0]["summary"]["travelTimeInSeconds"]);
var trafficDelay = Math.round(res["routes"][0]["summary"]["trafficDelayInSeconds"]/60);
var displayedTime = trafficTime + " +" + trafficDelay;
cell.getText().clear();
cell.getText().insertText(0,displayedTime);
You want to add a hyperlink to a text of a cell in a table on Google Slides.
You want to achieve this using Google Apps Script.
If my understanding is correct, how about this modification? Please think of this as just one of several possible answers.
Modification point:
In your case, TextRange returned from insertText can be used. For this, you can use the method of getTextStyle().setLinkUrl(URL).
Modified script:
When your script is modified, please modify as follows.
From:
cell.getText().insertText(0,displayedTime);
To:
var textRange = cell.getText().insertText(0,displayedTime);
textRange.getTextStyle().setLinkUrl(url);
or
cell.getText().insertText(0,displayedTime).getTextStyle().setLinkUrl(url);
In this casem, url is a string type like https://###sample###/.
References:
getTextStyle()
setLinkUrl(url)
If I misunderstood your question and this was not the direction you want, I apologize.

Google Sheets - Track Current Cell Selection?

How to Get the currently selected cell/range? was asked 3.5 years ago, but I'm hoping maybe some progress was made since then.
I'd like to make an interactive spreadsheet where there's a boolean grid that will display more information about the status of the currently selected cell in a static location, allowing users to quickly navigate using the arrow keys.
Referencing the previous instance of this question, I tried using the custom function below to get the cell address, although this isn't very dynamic.
function currentCell() {
return SpreadsheetApp.getActive().getActiveRange().getA1Notation();
}
When testing, this function returns the address of the cell the function is called from. I've tried pressing F5 while highlighting another cell and nothing happened. I've also dragged the formula across a selection and each cell only contains its own address (according to the documentation, it should contain the range).
I've also read Henrique's post on caching from 2012, and while I don't completely understand it, I get that we could not get scripts which update without edits being made or manual formula updates.
I would ideally like for the current cell to be tracked without any extra prompts from the user besides arrow key navigation or clicking to select a cell, but if pressing a non-altering key (like Enter) would allow for tracking, that would be fine. I also only need the selected cell, rather than a range, if that makes things easier.
Explicit Question: Is it possible to track the position of the currently selected cell in Google Sheets and store that position in a separate cell? If not, is there a way to do this without altering cell values?
EDIT: Adding an example for clarity. In this conceptual example, A2 =currentCell() as described above. If I were to hit the right arrow key, A2 would update to display I4, since it is now the current selection.
EDIT2: Jason Allshorn's response to Google app script monitor spreadsheet selected ranges appears to give a way of displaying the current selection (range) on the HTML input text. I'd like to do this, but store the value in a static cell on the sheet rather than input text. I'm new to scripting in Sheets and really only know what I've found for resolving this issue, so if I'm overlooking something, please let me know. I did look at Jason's example sheet and it wasn't clear to me what was actually happening there.
You will have to apply the technique suggested on How do I make a Sidebar display values from cells?. This consist on using using client-side code to constantly pulling values from the spreadsheet and then do the corresponding task.
One approach is to use a side-panel with an input field to set the cells to be tracked and another input field to set the destination cell.
You will have to use the onSelectionChange event in the app script to capture any selection change and then get the range name and put it in desired cell.
Something like the below:
function onSelectionChange(e) {
const range = e.range;
//Return in case of multi cell selection
if(range.getNumRows() !== 1 || range.getNumColumns() !== 1) return;
SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange("A2").setValue(range.getA1Notation());
}
You can see the official documentation here

Add custom Data to Google Sheet once Form is filled

I am using a Goofle form that receives applications from users, once the form is submitted I am using Google form submission trigger to notify. It has some questions like (facebook profile link)
But while sending a notification I don't want those links to be appeared just the same way user enters, instead something like Facebook, twitter (text as hyperlinked.) should appear
I already know about onFormsubmit() function, but not sure how to use, help me out please.
For example:
Cell A2 has user submitted facebook profile link using the form.
I want Column B2 to automatically generated as =hyperlink(A2, "Facebook")
The same thing should happen like A3 to B3 whenever user submits a form.
Ok after a bit of digging and testing, I believe I got a solution for you since you use the "Yet Another Mail Merge" (aka YAMM) add-on. Here it goes:
Firstly make sure your Form is setup properly and linked to a Google Sheet. After all questions have been created, add another column to your sheet, and call it 'Hyperlink' or whatever you please (just remember it for later). We will make use of the form submit trigger in the script editor along with some code.
Here's the code:
function onFormSubmit(e)
{
var r = e.range;
var v = e.namedValues;
var link = v['Link'];
// For testing purposes, this part was apart of my form,
// I'd assume you'd want to change it to something more
// usable in your case. Notice that I refer to the values
// by the name of the question they preside in.
var friendlyName = v['Friendly Name'];
var rngHyper = getCellRngByCol(r, 'Hyperlink');
// See below for the meaning of the boolean
addHyperlink(rngHyper, link, friendlyName, true);
}
// Will only return one cell no matter the range size.
// Perfect for onFormSubmit(e) use case.
function getCellRngByCol(rng, col)
{
var aRng = SpreadsheetApp.getActiveSheet().getDataRange();
var hRng = aRng.offset(0, 0, 1, aRng.getNumColumns()).getValues();
var colIndex = hRng[0].indexOf(col);
return SpreadsheetApp.getActiveSheet().getRange(rng.getRow(), colIndex + 1);
}
// Add some form of hyperlink reference to one particular
// cell, passed as a range object
function addHyperlink(rng, link, name, useFormula)
{
if (useFormula)
{
// If useFormula is TRUE, use Google Sheet HYPERLINK formula,
// only if you are sure all URL's are formated properly,
// and include HTTPS/HTTP/WWW. Also looks more pleasing in Google Sheet.
var formula = '=HYPERLINK("<<URL>>", "<<NAME>>")';
formula = formula.replace('<<URL>>', link).replace('<<NAME>>', name);
rng.setFormula(formula);
}
else
{
// Else use HTML <a> tag with hyperlink referencing, which should transform
// any URL passed as a clickable hyperlink within email. Not very visually
// appealing in Google Sheet.
var value = '<<NAME>>';
value = value.replace('<<URL>>', link).replace('<<NAME>>', name);
rng.setValue(value);
}
}
Then set the trigger, which will probably ask for authorization after saving:
Next, save the script and then put in a test submission through your form to see that the link is created properly, or as desired. Afterwards clear the rows of the spreadsheet (not the header) and remove all responses of the Form itself (not necessary, but keeps things organized for testing purposes).
Now, install the YAMM add-on. It should then add a new column at the end of your sheet called 'Merge satus'. Before setting up the email notification on submit, we need to create your email template. Open up GMAIL, create an email with the desired fields and layout and save it as a draft. Here's what I did as an example:
I'm sure you're familiar with how this add-on, works so I shouldn't need to explain too much here.
After the draft has been created and saved, go back to the Google Sheet attached to the Form. Go to Add-ons > YAMM > Configure form submission notifications. I chose the 'Notify one or more addresses of all responses' option which are tied to the 'To:' emails preset in the draft. Select your draft from the drop down, fill in sender name if needed, AND (very important!!) check the 'Wait until a specific column is filled before sending the email' check box. Make sure to select the Hyperlink column (or whatever you chose to name it earlier). Here's my setup for reference:
Save it, test it, and ta-da. This simple formatting for hyperlinks has been resolved! :D