Google Apps Scripts - Appending a paragraph after making a table? - google-apps-script

I'm pretty new to working with Google Apps Scripts - So I apologize in advance if the answer to this question is really obvious. Here goes:
I have created a Google Doc from a spreadsheet. I've selected specific columns from the spreadsheet to display in a nicely formatted table in a new created Google Doc. I'd like to append a paragraph after the table has been created. I'd even be happy to append an additional row with a cell to the end of the table. I want to do this so I can simple write a little reminder at the end of the document.
I've tried using table.appendTableRow( index) where index is the number of the last row generated but that doesn't seem to be working. I've tried using body.appendTable( table) and then appending a row and cell and paragraph but that isn't working. Does anyone have any suggestions?
Thanks in advance

Here you can add a row to the last table in your document.
var d = DocumentApp.getActiveDocument();
var body = d.getBody();
var tables = body.getTables();
tables[tables.length-1].appendTableRow().appendTableCell("yoyo");
If this is not what you are looking for please provide more details.
Hope it helps!

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

Need to create dynamic hyperlink or change cell remotely

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. :)

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.

Script to update Google Form from Spreadsheet

Here's my problem:
I can't seem to piece this info together nor find it anywhere even though it seems simple enough. When I find help, they just tell me to enter it manually (but my users can't be trusted) or make a new form (not an option).
What I need is to be able to keep:
the same google form
it's ID and link to the same spreadsheet
the same 20 "paragraph text" questions (titles remain the same).
There can't be any new questions or anything that would change the name or layout of the response destination page in the spreadsheet.
However, what I want is:
to update the help text below these questions from twenty cells in the spreadsheet. If my cells are from a range called questions!b2:b19 in the same spreadsheet, how do I use a script to take their contents and write over and update the help text in these paragraph questions on the google form?
Any help would be greatly appreciated.
It might be easiest to create your form in AppScript. With your form created, you can then use AppScript to access your spreadsheet. Read the information from the sheet and use it to create the help text on your questions with setHelpText.
Here's a basic sample.
var form = FormApp.create('New Form');
var item = form.addCheckboxItem();
var ss = SpreadsheetApp.openById("<ID>");
var val = ss.getRange(<RANGE>).getValue();
item.setTitle('Question');
item.setHelpText(val);
item.setChoices([
item.createChoice('ONE'),
item.createChoice('TWO'),
item.createChoice('THREE')
]);

Can I fill in TextItem by Google apps script?

I made a form with Google Form Builder, then I add a script for it.
I can run Session.getEffectiveUser().getEmail() to the respondent's email address. but I cant fill it in the textbox to assist them. Can I do such thing with Google App Script?
I think I found a solution to your problem. But I must admit it was not evident.
In apps scripts documentation you've got everything to create a prefilled url.
BUT for that you need to have a ItemResponse element and I didn't found any explaination to build one. The trick is when you've got an item you can get a ItemResponse from it if you get it as a defined type "asTextItem()".
Best way to understand it, is to watch the code below:
function getPreFilledItem(){
var form = FormApp.openById("YOUR_FORM_ID");
var items = form.getItems();
var itemOfInterest;
for(var i in items){
if(items[i].getTitle()=="YOUR_QUESTION_TITLE"){
itemOfInterest=items[i];
}
}
Logger.log(
form.createResponse().
withItemResponse(itemOfInterest.asTextItem().createResponse("PREFILLED_TEXT")).
toPrefilledUrl()
);
}
Hoping this will help you,
Harold
You can't dynamically modify Google Forms by attaching a script to the Form. See a recent question I asked to get a better understanding.
Basically, you can only use Google Apps Script to create forms in an automated manner. You can't set values for the items as of yet (I certainly hope they add this in the future...).
You can also see the limitations for Forms by looking at the documentation (TextItem, for example).