Skip to content

Instantly share code, notes, and snippets.

@krooluang
Created June 24, 2024 12:17
Show Gist options
  • Save krooluang/daa836b910a92b2e6d2ed03cb8eff9e6 to your computer and use it in GitHub Desktop.
Save krooluang/daa836b910a92b2e6d2ed03cb8eff9e6 to your computer and use it in GitHub Desktop.
GoogleAppscript Slider /ภาพสไลด์
function doGet() {
return HtmlService.createTemplateFromFile('index')
.evaluate()
.setTitle('Slider')
.addMetaTag('viewport', 'width=device-width, initial-scale=1')
.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL)
}
function getImagesFromSheet() {
const folderId = "xxx" //เปลี่ยนไอดีโฟลเดอร์
const folder = DriveApp.getFolderById(folderId);
const files = folder.getFiles();
const images = [];
while (files.hasNext()) {
const file = files.next();
if (file.getMimeType().startsWith('image/')) {
const url = `https://lh3.googleusercontent.com/d/${file.getId()}`;
images.push(url);
}
}
return images;
}
<!DOCTYPE html>
<html>
<head>
<title>Carousel Example</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h5 id="loading" class="mt-5 text-center">Loading...</h5>
<div id="carouselExampleIndicators" class="carousel slide" data-bs-ride="carousel">
<div class="carousel-indicators" id="carousel-indicators"></div>
<div class="carousel-inner" id="carousel-inner"></div>
<button class="carousel-control-prev" type="button" data-bs-target="#carouselExampleIndicators" data-bs-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="visually-hidden">Previous</span>
</button>
<button class="carousel-control-next" type="button" data-bs-target="#carouselExampleIndicators" data-bs-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="visually-hidden">Next</span>
</button>
</div>
</div>
<script>
$(document).ready(function() {
google.script.run.withSuccessHandler(setupCarousel).getImagesFromSheet();
});
function setupCarousel(images) {
let indicatorsHtml = '';
let innerHtml = '';
images.forEach((image, index) => {
indicatorsHtml += `
<button type="button" data-bs-target="#carouselExampleIndicators" data-bs-slide-to="${index}" ${index === 0 ? 'class="active" aria-current="true"' : ''} aria-label="Slide ${index + 1}"></button>
`;
innerHtml += `
<div class="carousel-item ${index === 0 ? 'active' : ''}">
<img src="${image}" class="d-block w-100" alt="Slide ${index + 1}">
</div>
`;
});
$('#carousel-indicators').html(indicatorsHtml);
$('#carousel-inner').html(innerHtml);
$('#loading').text('');
}
</script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.8/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.min.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment