Create Custom Buttons in Google Apps Script for Google Docs - google-apps-script

I am trying to find a way to create my own custom text for a ButtonSet in Google Apps Script. I do not want to use one of the default options of YES, NO, OK, etc. I would like the buttons to read: 'Today' and 'Tomorrow'. This is what I have tried so far, but I am having no luck...any thoughts on how I could accomplish this? Thank you!
// Create custom button text for Today and Tomorrow
var TodayButton = CardService.newTextButton()
.setText("Today");
var TomorrowButton = CardService.newTextButton()
.setText("Tomorrow");
var DayButtonSet = CardService.newButtonSet()
.addButton(TodayButton)
.addButton(TomorrowButton);
var responseWeatherImpactsDate = ui.alert('Do you want the first day of the Weather Impacts table to be today or tomorrow?', DayButtonSet);

Related

Set option 'Color' not working in google apps script? (pie chart builder)

Thank you for reading my question. I have been successfully using a script to create pie charts. However, yesterday the code to set pie chart slices' colors, setOption('colors',....), seemed to have stopped working. I have checked a few different types of code to fix it, but I am not able to fix anything related to it. I was wondering if its possible there is a change in the code and it can't be used this way any more? Please find below the code snippet as well as the link to the google sheet that has dummy data etc. Please do let me know how I can fix this? I'm a novice when it comes to coding so I'm a bit lost.
function createPieChart() {
var sheet = SpreadsheetApp.getActiveSheet();
var chartDataRange = sheet.getRange('G2:H5');
var color1 = sheet.getRange('K9').getValue();
var color2 = sheet.getRange('K10').getValue();
var color3 = sheet.getRange('K11').getValue();
var pieChartBuilder = sheet.newChart()
.addRange(chartDataRange)
.setChartType(Charts.ChartType.PIE)
.setPosition(2,1,0,0)
.setOption('width',500).setOption('height',300)
.setOption('pieHole',0.5)
.setOption('colors', [color1,color2,color3])
.build();
sheet.insertChart(pieChartBuilder);
}
Sheet Link: https://docs.google.com/spreadsheets/d/1tPc0KU2uYuN4rO32tW-s6lql_IW57kbDUdSR8ZSkVXY/edit#gid=0
You can report it on Google's Issue Tracker so they can take a look at it.
I have found a similar issue regarding charts with Apps Script, on this issue is about the option visibleInLegend. You can use it as reference to create your one or you can add to that issue: Chart option visibleInLegend not working on Apps Script

How to get an "image in cell" from a spreadsheet and copy it into a document as INLINE_IMAGE?

I've set-up a spreadsheet as a DB to store catalog-like information. An important element of this information is an image the end user inserts in a user-front sheet using the Google Spreadsheet's menu option "Insert --> Image --> Image in cell". This image is then copied into the storage sheet using "SpreadsheetApp.CopyPasteType.PASTE_VALUES" in a script.
Now I'm crafting a document as an output of the information stored in the DB. The issue is that I can't parse the image from the spreadsheet to the document. I'm not even being able to get it from the spreadsheet.
I've surfed the web for a solution but got nothing. The closest I found is a 2 year-old similar question without a direct response: how to copy IMAGE from a google spreadsheet to a google document in script?
My logic says that if I pasted the image-in-cell as a value, I should be able to get it back with .getValue() but obviously is not the case. I've tried to get it as a blob with no luck. I'm not too familiar with blobs so probably I did it wrong.
Any idea will be much appreciated :)
// This is the code I used to put the image in the cell (THIS WORKS, JUST FOR CONTEXT)
var fotosToCopy = cotizador.getRangeByName('CotizacionFotos');
fotosToCopy.copyTo(destino.getRange(fila,2),
SpreadsheetApp.CopyPasteType.PASTE_VALUES, true);
// This is the code I'm trying to get the image from the cell (NOT WORKING)
var fotosToCopy = origen.getRange(personajeRow,2).getValue(); //I've tried .getFormula() with no results; origen is the search range, personajeRow is the row to do the search and both work.
// This is what I'm using to put the image in the document
var doc = DocumentApp.openById(cotizacionId); // cotizacionId is working
var body = doc.getBody();
var foto = body.getImages(); // I'll replace the only existing image
var parent = foto[0].getParent();
parent.insertInlineImage(parent.getChildIndex(foto)+1, fotosToCopy[0]); //fotosToCopy[0] is the issue, it returns "undefined"
foto[0].removeFromParent();
Ideally, the existing in-line-image in the document should be replaced by the image-in-cell from the spreadsheet. The error I get is: "Execution failed: Cannot convert Array to Element".
Any idea?
so I haven't coded this very cleanly but it should give you somehtign close to what you want, but first an explanation:
Google Sheets doesn't have the capacity to return the blob of an image, only a reference to the image, but Google Drive does.
My solution below takes the blob of the FIRST image in the Google Drive folder and adds it to the Document based on the DocID I provide to the function:
function retriveFolder(folderName,imageName,docID){
var imageFolder = DriveApp.getFoldersByName(folderName).next();
var firstImageinFolder = imageFolder.getFilesByName(imageName).next();
var imageBlob = firstImageinFolder.getBlob();
DocumentApp.openById(docID).getBody().insertImage(1, imageBlob);
}
I hope this helps, best I could come up with when I had the same problem. Not ideal but should be a suitable workaround.

Is it possible to add tooltips to google chart using only Class EmbeddedChart?

I have tried to ask a question here where I'd use an embedded chart in gSheets to add tooltips. An answer came back that doesn't work. It DID, however, lead me to search through "google.visualization.DataTable()". This only get me more confused, as I think that creates the chart in another html page.
So, now I'm restating my question to see if what I want is even possible.
GOAL: Have a chart in existing gSheet use custom tooltips from another column like these nifty, dynamic tooltips
PROBLEMS/MY DEFICIENCIES:
1) Many examples on StackOverflow show adding the data in the code itself. Here's one using addTable I need to use the data in my sheet and I don't know how to reference/call it.
2) Most guides in Google use depreciated Uiapp service , and I can't figure out how to copy/learn their code to translate to the new HTML service. Github example
3) If I was to try to use google.visualization.DataTable, it gives me an error, making me think that it wants a more code, and other html files. I can't find the tutorials on HOW to do this. I'm willing to try but can't find the right search terms.
4) Is there a better way to do this? All videos/articles show from 2014 and older.
My code:
function addNewRange) {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('Q:Q').activate();
var sheet = spreadsheet.getActiveSheet();
var charts = sheet.getCharts();
var chart = charts[0];
sheet.removeChart(chart);
chart = sheet.newChart()
.asScatterChart()
.addRange(spreadsheet.getRange('S3:S50004'))
.addRange(spreadsheet.getRange('T3:S50004'))
.addRange(spreadsheet.getRange('Q3:Q50004')) // This the line I need to use as the tooltips
.setOption('useFirstColumnAsDomain', false)
.setPosition(2, 7, 0, 0)
.build();
sheet.insertChart(chart);
};

Display Data from Google Sheet into Google Forms

I am creating a Quiz.
What I did first is to create data in google sheet and here it is
Link of Google Sheet
as you can see there I have 2 sheet and that is Questions and Answers
My Question is how can i display the Question and Answers in there respective position in Google Forms i am linking them by Question ID
TYSM
I think the comments above will help you. This is also code (with help from here) which copies a multiple-choice question from one quiz to another.
If you combine this code with the stuff referred to in the comments about reading stuff from a sheet then you should be ok.
I see that you mention a problem copying images. I'm stuck at that point too.
function copyMultipleChoiceItem(item1, item2) {
// copies MC question item1 to item2 - tested PDW 17/05/20
// copy of feedback now working - tested PDW 17/05/30
//
var item1MC = item1.asMultipleChoiceItem();
// basic question items
item2.setTitle(item1.getTitle());
item2.setHelpText(item1.getHelpText());
item2.setPoints(item1MC.getPoints());
item2.setRequired(item1MC.isRequired());
// the choices
var choices = item1MC.getChoices();
for (var i = 0; i < choices.length; i++) {
item2.createChoice(choices[i].getValue(),choices[i].isCorrectAnswer());
}
item2.setChoices(choices);
// the feedback
var feedback1 = item1MC.getFeedbackForCorrect();
item2.setFeedbackForCorrect(feedback1);
var feedback1 = item1MC.getFeedbackForIncorrect();
item2.setFeedbackForIncorrect(feedback1);
}
I'm not fully cleared about your question, but If you want an easy way to do a relationship between Google Sheets (columns) and Google Forms Drop-down lists and other Forms objects, you can try this add-on: formRange. Build your sheets then go to your Form and Addons. Chose formRange and install it. It's pretty easy to use and you have a little tutorial inside it.
formRanger: Google Form addon

How do I open a web browser using google apps script? [duplicate]

This question already has answers here:
Google Apps Script to open a URL
(6 answers)
Closed 2 years ago.
I've created a spreadsheet in google drive, and I've put together a tutorial presentation using google presentations to demonstrate to the users how to use the spreadsheet and why it's better than the way we were doing it before.
What I would like is a message box to appear when the spreadsheet is opened asking if that person has watched the tutorial presentation yet. If the user clicks No I'd like to either open a new page in the browser to show them the presentation I've published, or show the presentation in a custom UI.
I've searched for hours and can't figure it out. Is this possible? Thank you for your help!
I'm new to the forum so please let me know if I posted this wrong. Thanks!
Something like this does it pretty well.
I used a fake button to keep the "look and feel" consistent with the other button , I simply added the link invisible on top of it ;-)
function alertLink() {
var doc = SpreadsheetApp.getActiveSpreadsheet();
var app = UiApp.createApplication().setTitle('Message').setHeight('100').setWidth('400');
var panel = app.createVerticalPanel().add(app.createHTML('Did you see my beautiful <b>Tutorial</b> ?'));
var grid = app.createGrid(1,2).setWidth('400');
var closeHandler = app.createServerHandler('close');
var b1 = app.createButton("NO and I'd like to").setTitle('go to the tutorial in a new tab');
var b2 = app.createButton("YES and I don't want to see it again",closeHandler).setTitle('close this window');
var link = app.createAnchor('XXXXXXXXXX','http://www.google.com').setStyleAttributes({'zIndex':'1' , 'position':'fixed' , 'top':'25' , 'left':'20', 'color':'transparent' }).setTitle('go to the tutorial in a new tab');
var G1 = app.createVerticalPanel().add(b1).add(link);
grid.setWidget(0,0,G1).setWidget(0,1,b2);
app.add(panel).add(grid)
doc.show(app)
}
function close(){
return UiApp.getActiveApplication().close();
}
Just use an onOpen trigger if you want it to execute automatically on spreadsheet open. (or rename the main function as onOpen() )
Here is how it looks like :
For an onOpen trigger, it's certainly a better UX to prompt the user with a dialog box offering the option to open a URL, as opposed to opening the URl automatically. See serge's answer here for how to do that (which is more up-to-date than the deprecated code in his answer on this thread).
But for other triggers (such as a menu item click) check out my answer here to open a URL automatically.