How to import data from Google sheets to Slides - javascript

I am trying to create a button in google sheets that run a script every time it is triggered. The script should first create a new copy of the template and then using that google sheet replace all placeholder text in that presentation with data from sheets. I am not sure why the code is not working as of now.
// The following creates the UI button in sheets (This works)
function onOpen() {
let ui = SpreadsheetApp.getUi();
ui.createMenu('Create Report')
.addItem('Create Report', 'executeAll')
.addToUi();
}
function executeAll (){
var reportTemplate = DriveApp.getFileById('Presentation ID goes Here');
var copiedTemplate = reportTemplate.makeCopy('New Report', DriveApp.getFolderById("Folder ID Goes here"));
var skeleton = SlidesApp.openById(copiedTemplate.getId());
var slides = skeleton.getSlides();
return slide1();
function slide1 (){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('App Script Input Sheet');
var data = sheet.getRange('E1:F26').getValues;
var slide1 = slides[0];
var newslide1 = slide1.duplicate();
var shapes = (newslide1.getShapes());
shapes.forEach(function(shape){
shape.getText().replaceAllText('{{Date}}',data[2]);
shape.getText().replaceAllText('{{Title}}',data[3]);
shape.getText().replaceAllText('{{Value 1}}',data[4]);
shape.getText().replaceAllText('{{Value 2}}',data[5]);
//there are more to be replaced. will add once code works.
});
}
}

Related

How do you make Text value equal a Variable value using google apps script in the google documents?

im making a system to edit the text as a viewer, but I can't figure it out!
Here's my code so far:
function onOpen(e) {
DocumentApp.getUi()
.createMenu('Check')
.addItem('Check', 'check')
.addToUi();
}
function check() {
var ui = DocumentApp.getUi();
var result = ui.prompt("Enter change text");
Logger.log(result.getResponseText());
Logger.getLog
var body = DocumentApp.getActiveDocument().getBody();
var qwerty = DocumentApp.insertText.insertText();
}
I was expecting it to add text, but it just gave me an error.

how to make this translation function code by google apps-script?

I really want to add a translation menu on my google sheet but I definitely don't know what should I do at the last part.
function onOpen(e) {
SpreadsheetApp.getUi()
.createMenu('Translation')
.addItem('ko to eng', 'myFunction')
.addToUi();
}
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getActiveRange();
var trans = LanguageApp.translate(sheet,'ko','en');
that is it for now.
I want to make 'myFuncton' that can spread var 'trans' at the same place in the sheet.
how can I code this?
If you'd like to print the translation e.g. on cells to the right of the selected cells, you can use this function:
function myFunction () {
var range = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getActiveRange();
var trans = range.getValues().map(row => [LanguageApp.translate (row,'ko','en')])
range.offset(0,1).setValues(trans)
}

Get link to sheet in image

how can i go to a specific sheet when i click on the image? I need to go to the "FERMENTADOR 2_002 / 2021" sheet when the user clicks on the "fermenter" image.
the problem is that the name of the sheet may change according to some criteria ... so the sheet to be activated needs to be with the value of cell A4 + the value of cell B16
function hyperlink(){
var sheet = SpreadsheetApp.getActiveSheet();
var nomeferm = sheet.getRange("A4").getValue();
var numlote = sheet.getRange("B16").getValue();
var pagina = nomeferm+"_"+numlote;
var tt = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(pagina);
}
Updated Code based on your comment:
function getToSheet() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('HOME');
const name = sh.getRange('A4').getValue()+"_"+sh.getRange('B16').getValue();
const sheet = ss.getSheetByName(name);
if(sheet){
ss.setActiveSheet(sheet, true);
}
}
Solution:
I guess the most straightforward way is to attach the following very simple script to your image:
function getToSheet() {
const ss = SpreadsheetApp.getActive();
ss.setActiveSheet(ss.getSheetByName('FERMENTADOR 2_002/2021'), true);
}
The script will activate sheet FERMENTADOR 2_002/2021 therefore it will redirect you there.
You just need to assign this script to your image and then you can click on the image itself (see attached gif).
Illustration:

Chart disappears after page submit

I was trying to build an Oracle Apex 5.1 app with new mobile theme which has a collapsable dynamic content region.
Behind this region there's a pl/sql code that prints out a chart codelet into the HTML body which consists of a "host" div and a JS script block that generates the chart (AnyChart 7.14.4) into it.
Everything works fine on the first page visit but when I press a region button (in an another, static region) which submits the page via dynamic action, the chart's completely gone (although the div stays spanned to its original size).
Do you have any idea what's behind this symptom?
!UPDATE!
This JS function is generated by a page process (Pre-rendering/before regions):
function createChart() {
anychart.theme("darkEarth");
var dataSet = [
["2006.10.17",212.82,22.58,3.10,2.79],
["2006.10.18",212.04,22.57,3.10,2.81],
["2006.10.19",208.46,22.40,3.06,2.78],
["2006.10.20",208.60,22.43,3.06,2.78],
["2006.10.23",210.47,22.56,3.08,2.81],
["2006.10.24",209.18,22.57,3.09,2.80],
["2006.10.25",208.00,22.50,3.08,2.78],
["2006.10.26",205.22,22.35,3.04,2.76],
["2006.10.27",204.89,22.26,3.04,2.76],
["2006.10.30",205.93,22.20,3.05,2.76],
["2006.10.31",204.41,22.00,3.03,2.75],
];
var seriesList = anychart.data.mapAsTable(dataSet);
var chart = anychart.line();
chart.crosshair().enabled(true).yLabel().enabled(false);
chart.crosshair().yStroke(null);
chart.tooltip().positionMode("point");
chart.yAxis().title("%");
var credits = chart.credits();
credits.enabled(false);
chart.animation(true);
chart.title().enabled(false);
chart.xAxis().labels().padding([5]);
var series_1 = chart.line(seriesList[0]);
series_1.name("HUF");
var series_2 = chart.line(seriesList[1]);
series_2.name("CZK");
var series_3 = chart.line(seriesList[2]);
series_3.name("PLN");
var series_4 = chart.line(seriesList[3]);
series_4.name("RON");
for (i = 0; i < chart.getSeriesCount(); i++)
{
chart.getSeriesAt(i).hoverMarkers().enabled(true).type("circle").size(4);
chart.getSeriesAt(i).tooltip().position("right").anchor("left").offsetX(5).offsetY(5);
}
chart.legend().enabled(true).fontSize(13);
chart.container("chartContainer");
chart.draw();
}
This is followed by a static region in position body2 with the following content:
<div id="chartContainer"></div>
<script type="text/javascript">
anychart.onDocumentReady(function() {
createChart();
});
</script>
We are afraid that this issue goes beyond AnyChart area responsibility. Also, we've never met a similar problem. We would recommend you to forward this query to Oracle tech support.
I did a test page here: https://apex.oracle.com/pls/apex/f?p=145797:18
Login on: https://apex.oracle.com/pls/apex/f?p=4550
workspace: stackquestions
login: test
pwd: test
app: 145797
page: 18
In this page I have a process on "Pre-Rendering" > "Before Regions"
begin
htp.p(
'<script>
function createChart() {
anychart.theme("darkEarth");
var dataSet = [
["2006.10.17",212.82,22.58,3.10,2.79],
["2006.10.18",212.04,22.57,3.10,2.81],
["2006.10.19",208.46,22.40,3.06,2.78],
["2006.10.20",208.60,22.43,3.06,2.78],
["2006.10.23",210.47,22.56,3.08,2.81],
["2006.10.24",209.18,22.57,3.09,2.80],
["2006.10.25",208.00,22.50,3.08,2.78],
["2006.10.26",205.22,22.35,3.04,2.76],
["2006.10.27",204.89,22.26,3.04,2.76],
["2006.10.30",205.93,22.20,3.05,2.76],
["2006.10.31",204.41,22.00,3.03,2.75],
];
var seriesList = anychart.data.mapAsTable(dataSet);
var chart = anychart.line();
chart.crosshair().enabled(true).yLabel().enabled(false);
chart.crosshair().yStroke(null);
chart.tooltip().positionMode("point");
chart.yAxis().title("%");
var credits = chart.credits();
credits.enabled(false);
chart.animation(true);
chart.title().enabled(false);
chart.xAxis().labels().padding([5]);
var series_1 = chart.line(seriesList[0]);
series_1.name("HUF");
var series_2 = chart.line(seriesList[1]);
series_2.name("CZK");
var series_3 = chart.line(seriesList[2]);
series_3.name("PLN");
var series_4 = chart.line(seriesList[3]);
series_4.name("RON");
/*for (i = 0; i < chart.getSeriesCount(); i++)
{
chart.getSeriesAt(i).hoverMarkers().enabled(true).type("circle").size(4);
chart.getSeriesAt(i).tooltip().position("right").anchor("left").offsetX(5).offsetY(5);
}*/
chart.legend().enabled(true).fontSize(13);
chart.container("chartContainer");
chart.draw();
}</script>'
);
end
I put the files .js on "HTML Header"
And the HTML on the source of a region
Could you replicate your problem in this page?

Google Spreadsheet Custom Menu Returned Script Function Not Found

I am trying to add a custom menu to the google spreadsheet editor's interface to activate a script by Tony Hirst to pull calendar events into the spreadsheet.
By when I click on the custom menu option, it returned "script function not found: GetCalendar()"
I have tried to make modifications and read the google documentation but am not sure what I am doing wrong.
Thank you.
/**
* A custom function that gets the calendar events by calendar ID.
*/
function GetCalendar() {
// get the spreadsheet object
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// set the second sheet as active
SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[1]);
// fetch this sheet
var sheet = spreadsheet.getActiveSheet();
//http://www.google.com/google-d-s/scripts/class_calendar.html#getEvents
// The code below will retrieve events between 2 dates for the user's default calendar and
// display the events the current spreadsheet
var calId = "SpecificCalendarID";
var cal = CalendarApp.getCalendarById(calId);
var events = cal.getEvents(new Date("June 15, 2015"), new Date("March 20, 2080"));
for (var i=0;i<events.length;i++) {
//http://www.google.com/google-d-s/scripts/class_calendarevent.html
var details=[[events[i].getTitle(), events[i].getDescription(), events[i].getStartTime()]];
var row=i+2;
var range=sheet.getRange(row,1,1,3);
range.setValues(details);
}
}
/**
* A function that runs when the spreadsheet is open, used to add a
* custom menu to the spreadsheet.
*/
function onOpen() {
var ui = SpreadsheetApp.getUi();
var menuItems = ui.createMenu ('Custom Menu');
menuItems.addItem ('Get Events','GetCalendar()');
menuItems.addToUi();
}
Remove the parenthesis from GetCalendar()
Should be:
menuItems.addItem ('Get Events','GetCalendar');

Categories

Resources