メインコンテンツにスキップ

gas-url-shortener

A URL Shortening service powered by Google Apps Script.

AI Summary

TakeMeThere (GAS URL Shortener)

Template for building a custom-domain URL shortener with Google Apps Script, Google Sheets, and GitHub Pages.

Target Users

Intermediate GAS developers who need a server-less, custom-domain URL shortener for internal tools or marketing links.

Problems Solved

Running a branded URL shortener usually requires servers and database management, increasing cost and complexity.

Tags

Main Features

1
Zero server cost

Runs entirely on free tiers of GAS and GitHub Pages, eliminating infrastructure maintenance.

2
Spreadsheet-based management

Mappings are stored in Google Sheets, allowing easy viewing and editing.

3
Custom domain with HTTPS

Leverages GitHub Pages’ automatic SSL to serve short links over HTTPS on your own domain.

Usage Examples

Basic redirect handler with doGet

/**
 * Column A = slug, Column B = long URL in the spreadsheet
 */
const SHEET_ID  = 'YOUR_SHEET_ID_HERE';
const SHEET_NAME = 'URLs';

/**
 * Redirects to the long URL that matches the requested slug.
 * @param {GoogleAppsScript.Events.DoGet} e
 */
function doGet(e) {
  const slug = (e.pathInfo || '').trim();      // e.g. /abc → "abc"
  if (!slug) {
    return HtmlService.createHtmlOutput('Slug not provided.');
  }

  const sheet = SpreadsheetApp.openById(SHEET_ID).getSheetByName(SHEET_NAME);
  const data  = sheet.getRange(2, 1, sheet.getLastRow() - 1, 2).getValues();

  const hit = data.find(row => row[0] === slug);
  if (hit) {
    const longUrl = hit[1];
    const html = `<script>window.top.location.replace('${longUrl}');</script>`;
    return HtmlService.createHtmlOutput(html).setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
  }

  return HtmlService.createHtmlOutput('URL not found');
}

Deploy this script as a web app (accessible by "Anyone, even anonymous").
Visiting https://your-script-id/exec/{slug} will redirect the browser to the long URL stored in the spreadsheet.