Last active
January 17, 2021 12:56
-
-
Save ikemo3/c2279be7ba4c5f253acbf42b13280750 to your computer and use it in GitHub Desktop.
GoogleスプレッドシートでCtrl + Eを使う(TamperMonkey対応)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Emacs Ctrl + E | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description Enable Ctrl + E(go to end-of-line) to Google Spreadsheet | |
// @author Hideki Ikemoto | |
// @match https://docs.google.com/spreadsheets/d/* | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
var inputCell; | |
function addEvent() { | |
const editableCells = document.querySelectorAll("[contenteditable='true']"); | |
editableCells.forEach((cell) => { | |
cell.addEventListener("keydown", eventHandler); | |
}); | |
} | |
function getInputCell() { | |
// 数式入力バーが表示されるのを待つ | |
inputCell = document.querySelector("#t-formula-bar-input-container div.cell-input"); | |
if (inputCell) { | |
setTimeout(addEvent, 200); | |
} else { | |
setTimeout(getInputCell, 200); | |
} | |
} | |
function getEndOffset(str, startOffset) { | |
// 改行が出るまで検索(絵文字などはUTF-16のコードポイントのまま) | |
for (let i = startOffset; i < str.length; i++) { | |
if (str[i] === "\n") { | |
return i; | |
} | |
} | |
return str.length; | |
} | |
function eventHandler(e) { | |
if (e.ctrlKey === true && e.key === "e") { | |
const selection = window.getSelection(); | |
const range = selection.getRangeAt(0); | |
const newRange = document.createRange(); | |
const childNode = e.target.childNodes[0]; | |
if (childNode instanceof Text) { | |
newRange.setStart(childNode, childNode.length); | |
} else if (childNode.nodeName == "SPAN") { | |
const textNode = range.startContainer; | |
const startOffset = range.startOffset; | |
const endOffset = getEndOffset(textNode.textContent, startOffset); | |
newRange.setStart(textNode, endOffset); | |
} else { | |
// bdo -> span -> spanのリスト | |
const spanList = childNode.childNodes[0].childNodes | |
const lastSpan = spanList[spanList.length - 1]; | |
newRange.setStart(lastSpan.firstChild, lastSpan.textContent.length); | |
} | |
selection.removeAllRanges(); | |
selection.addRange(newRange); | |
} | |
} | |
setTimeout(getInputCell, 1000); | |
})(); |
クリックなしに空のセルに入力し始めると、Ctrl + Eでエラーになるのが直った・・・はず
動きました!使わせていただきありがとうございます!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
数式が入っている場合に動いていなかったのを修正。