I am trying to create dynamic app script components that can be added into my site with dynamic data for each instance of the script. I tried doing parameters, but I am not sure if that’s the best way to approach this. For example, I want to create an image script that be loaded with dynamic links, and inserted into google sites. I only want one image script can be loaded multiple times into the page with dynamic urls. How should I handle this? Can this be done? Thanks.
Advertisement
Answer
This version creates scrolling images display or a slide show. And it creates the image tags in an otherwise empty div. You can add as many images to column A of your spreadsheet and the script will do the rest.
image.html:
JavaScript
x
73
73
1
<!DOCTYPE html>
2
<html>
3
<head>
4
<base target="_top">
5
</head>
6
<body>
7
<div id="myimages"></div>
8
<div id="slideshow" style="display:none;">
9
<img id="slide" src="" width="450"/>
10
</div>
11
<input type="button" value="Start Slide Show" onClick="startShow();" />
12
<input type="button" value="Stop Show" onClick="stopShow();" />
13
<div id="resp" style="display:none;">
14
<h1>Response</h1>
15
<p>Your data has been received.</p>
16
</div>
17
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
18
<script>
19
var nextslide=0;
20
var myslides='';
21
var mytimer;
22
$(function() {
23
google.script.run
24
.withSuccessHandler(setURL)
25
.getURL();
26
});
27
function setURL(urlA)
28
{
29
for(var i=0;i<urlA.length;i++)
30
{
31
var s='img' + Number(i+1);
32
var s1= '#img' + Number(i+1);
33
$('#myimages').append('<img id="' + s + '" src="' + urlA[i] + '" />');
34
$(s1).attr('height','450');
35
}
36
myslides=urlA;
37
}
38
39
function startShow()
40
{
41
$('#myimages').css('display','none');
42
$('#slideshow').css('display','block');
43
showSlide();
44
}
45
46
function showSlide()
47
{
48
document.getElementById('slide').src=myslides[nextslide];
49
if(++nextslide > myslides.length-1)
50
{
51
nextslide=0;
52
}
53
mytimer=window.setTimeout(showSlide,5000);
54
}
55
56
function stopShow()
57
{
58
window.clearTimeout(mytimer);
59
$('#myimages').css('display','block');
60
$('#slideshow').css('display','none');
61
62
}
63
64
function loadTxt(from,to)
65
{
66
document.getElementById(to).value = document.getElementById(from).value;
67
}
68
69
console.log('My Code');
70
</script>
71
</body>
72
</html>
73
Code.gs
JavaScript
1
30
30
1
function doGet()
2
{
3
var html = HtmlService.createHtmlOutputFromFile('image');
4
return html.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL)
5
6
}
7
8
function getData(a)
9
{
10
var ts = Utilities.formatDate(new Date(), "GMT-6", "yyyy-MM-dd' 'HH:mm:ss");
11
a.push(ts);
12
var ss=SpreadsheetApp.openById('SS_ID')
13
ss.getSheetByName('Form Data').appendRow(a);
14
return true;
15
}
16
17
function getURL()
18
{
19
var ss=SpreadsheetApp.openById('SS_ID');
20
var sht=ss.getSheetByName('imgURLs');
21
var rng=sht.getDataRange();
22
var rngA=rng.getValues();
23
var urlA=[];
24
for(var i=1;i<rngA.length;i++)
25
{
26
urlA.push(rngA[i][0]);
27
}
28
return urlA;
29
}
30