Published Google Sheets pubhtml link to XLSX file

Add a button on pubhtml pages to download the sheet as an XLSX file.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @license MIT
// @name        Published Google Sheets pubhtml link to XLSX file
// @namespace   ViolentmonkeyScripts
// @match       https://docs.google.com/spreadsheets/d/e/*/pubhtml*
// @grant       none
// @version     1.0
// @description Add a button on pubhtml pages to download the sheet as an XLSX file.
// ==/UserScript==

(function () {
  'use strict';

  const ID = 'vm-download-xlsx-btn';
  if (document.getElementById(ID)) return;

  function buildDownloadUrl(href) {
    // Replace trailing /pubhtml and any query string with /pub?output=xlsx
    return href.replace(/\/pubhtml(\?.*)?$/, '/pub?output=xlsx');
  }

  function createButton() {
    const btn = document.createElement('button');
    btn.id = ID;
    btn.type = 'button';
    btn.textContent = 'Download XLSX';
    Object.assign(btn.style, {
      position: 'fixed',
      top: '12px',
      right: '12px',
      zIndex: 99999,
      padding: '8px 12px',
      borderRadius: '6px',
      border: 'none',
      boxShadow: '0 2px 6px rgba(0,0,0,0.15)',
      background: '#1a73e8',
      color: '#fff',
      fontSize: '13px',
      cursor: 'pointer',
    });

    btn.addEventListener('click', (e) => {
      e.preventDefault();
      const downloadUrl = buildDownloadUrl(location.href);
      window.open(downloadUrl, '_blank');
    });

    document.body.appendChild(btn);
  }

  // Some pubhtml pages may load late; try to insert immediately and also after load.
  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', createButton, { once: true });
  } else {
    createButton();
  }

  // If page dynamically replaces body, re-add button if needed.
  const mo = new MutationObserver(() => {
    if (!document.getElementById(ID) && location.href.includes('/pubhtml')) createButton();
  });
  mo.observe(document.documentElement, { childList: true, subtree: true });
})();