Google Sheets Enable Scandinavian Keyboard On Mac

Fix shift+option+number keys in Google Sheets on Mac while using a Scandinavian keyboard layout, by interception of shift+option+number keys, and replace them with the correct char pasted in. Flaws: Does not enter edit mode when inserting into inactivated sheet cells.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Google Sheets Enable Scandinavian Keyboard On Mac
// @namespace    https://gist.github.com/f-steff
// @version      1.3
// @description  Fix shift+option+number keys in Google Sheets on Mac while using a Scandinavian keyboard layout, by interception of shift+option+number keys, and replace them with the correct char pasted in. Flaws: Does not enter edit mode when inserting into inactivated sheet cells.
// @author       Flemming Steffensen
// @match        http://docs.google.com/spreadsheets/*
// @match        https://docs.google.com/spreadsheets/*
// @include      http://docs.google.com/spreadsheets/*
// @include      https://docs.google.com/spreadsheets/*
// @grant        none
// @license      MIT
// @homepageURL  https://gist.github.com/f-steff/ace84434e1ee4e1107bcf0ba8d72ed2b
// @run-at document-start
// ==/UserScript==


function simulatePaste(code, character) {
    navigator.clipboard.writeText(character).then(() => {
        document.execCommand('paste');
    });
}

(function() {
    'use strict';

    var isMac = navigator.platform.indexOf("Mac") === 0
    if (!isMac) return; // Only a fix for Mac computer

    // Store the original addEventListener function
    const originalAddEventListener = EventTarget.prototype.addEventListener;

    // To my supprise, browser keyboard events are unfiltered hardware events, so they need debounching.
    const debounceTime = 50; // milliseconds
    let lastInvocationTime = 0;

    // Hack: Override addEventListener on the EventTarget prototype
    EventTarget.prototype.addEventListener = function(type, listener, options) {
        const isPassive = options && (typeof options === 'object') && options.passive;
        const customListener = (event) => {
            if (type === "keydown" && event.shiftKey && event.altKey && event.code.startsWith("Digit")) {
                if (!isPassive) {
                    event.preventDefault(); // Prevent the default key action
                }
                const currentTime = new Date().getTime();
                if (currentTime - lastInvocationTime > debounceTime) {

                    console.log("Current: " + currentTime + " Last: " + lastInvocationTime + " = Shift and Option are pressed together with " + event.code);
                    lastInvocationTime = currentTime;

                    // Handle keys
                         if (event.code === "Digit1") { simulatePaste(event.code, "¯"); } //  Sheets mapping is: upper border set
                    else if (event.code === "Digit2") { simulatePaste(event.code, "”"); } //  Sheets mapping is: right border set
                    else if (event.code === "Digit3") { simulatePaste(event.code, "$"); } //  Sheets mapping is: lower border set
                    else if (event.code === "Digit4") { simulatePaste(event.code, "¢"); } //  Sheets mapping is: left border set
                    else if (event.code === "Digit5") { simulatePaste(event.code, "‰"); }
                    else if (event.code === "Digit6") { simulatePaste(event.code, "˜"); } //  Sheets mapping is: all border clear
                    else if (event.code === "Digit7") { simulatePaste(event.code, "\\"); } // Sheets mapping is: all border set
                    else if (event.code === "Digit8") { simulatePaste(event.code, "{"); }
                    else if (event.code === "Digit9") { simulatePaste(event.code, "}"); }
                    else if (event.code === "Digit0") { simulatePaste(event.code, "≈"); }
                } else return;
            } else {
                listener.call(this, event);
            }
        };
        // Call the original addEventListener function
        originalAddEventListener.call(this, type, customListener, options);
    };
})();