Google 文档的自定义键盘快捷键(更改颜色 - 背景颜色)
Custom keyboard shortcuts for Google Docs (change color - background color)
我想使用键盘快捷键更改 Google 文档中所选文本的 ForegroundColor
。
我可以制作 "change the ForegroundColor" 部分(菜单项绑定到函数 setColor() ),但不能制作 "keyboard shortcut part"。
我找到了这个 code 但我很难实现它:
$(document).keydown(function(e){
//CTRL + Q keydown combo
if(e.ctrlKey && e.keyCode == 81){
$( '#output' ).html("I've been pressed!");
}
})
我的难处:
1) 我不确定将这段代码放在我的脚本编辑器中的什么位置(我尝试将它放在 onOpen()
函数中,如下所示,但也没有成功)。
2) 我不确定 $(document) 应该指的是什么。
3) 我不确定 "having to click on / activate the sidebar first for that to happen" 是什么意思。
function onOpen() {
var ui = DocumentApp.getUi();
ui.createMenu('My Menu')
.addItem('Color', 'setColor')
.addToUi();
var document = DocumentApp.getActiveDocument() // should it be here?
$(document).keydown(function(e){
//CTRL + Q keydown combo
if(e.ctrlKey && e.keyCode == 81){
SpreadsheetApp.getUi().alert('Hello, world!');
}
})
}
function setColor1() {
var selection = DocumentApp.getActiveDocument().getSelection();
if (selection) {
var elements = selection.getRangeElements();
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
// Only modify elements that can be edited as text; skip images and other non-text elements.
if (element.getElement().editAsText) {
var text = element.getElement().editAsText();
// Edit the selected part of the element, or the full element if it's completely selected.
if (element.isPartial()) {
text.setForegroundColor(element.getStartOffset(), element.getEndOffsetInclusive(), "#00FFFF");
} else {
text.setForegroundColor("#00FFFF");
}
}
}
}
}
在这一行中,变量 'document' 是 Google Apps 脚本文档中定义的 'Document' 对象的一个实例。
var document = DocumentApp.getActiveDocument()
它是对位于您云端硬盘中的 Google 文档的抽象,允许您使用此处描述的一组方法修改您的云端硬盘文件 https://developers.google.com/apps-script/reference/document/document
下面这行是jQuery语法。
$(document).keydown(function(e){}
jQuery 是一个用于客户端开发的 JavaScript 库。它用于导航网页的 HTML DOM 树。 $(document) 是 jQuery 对 DOM 的表示。更多关于 jQuery http://jquery.com/
因为 Google Apps 脚本在 Google 服务器上运行,而不是在您的本地计算机上运行,所以没有 DOM 树供您导航。您仅限于映射到 Google 服务器上的代码位的 GAS 函数。这两行代码彼此没有任何关系,也不是指同一个文档。
这同样适用于您可以收听的活动。虽然 GAS 确实具有一些事件处理功能,但事件列表非常短(参见 'Simple Triggers' 和可安装触发器')https://developers.google.com/apps-script/guides/triggers/
更新
您可以通过使用 Html 服务构建自己的自定义 UI 来实现此目的。需要注意的是,似乎无法将控制权从 Google Document 移交给 UI 元素,因此您必须在每次操作后手动将焦点设置在侧边栏上。
下面是项目中 .gs
文件的示例。将此视为您的服务器应用程序。
//creates html output from the template
function onOpen(){
var ui = DocumentApp.getUi();
var htmlOutput = HtmlService.createTemplateFromFile('sidebar').evaluate();
ui.showSidebar(htmlOutput);
}
function setColor1(){
//your code
}
下面是侧边栏模板的客户端代码。您可以通过单击文件 -> 新建 -> Html 文件在脚本编辑器中创建模板。
在上面的 .gs
文件中,将其命名为 sidebar
或您为 var htmlOutput
选择的任何名称。如果您按 'Ctrl + Q',边栏将显示确认信息并调用 .gs
文件中的 setColor1()
函数。
更多关于从客户端调用服务器端 GAS 函数的信息https://developers.google.com/apps-script/guides/html/reference/run
有关Html服务的更多信息
https://developers.google.com/apps-script/guides/html/
明显的缺点是侧边栏必须先获得焦点,所以你总是需要在选择后点击它。
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<p> Press Ctrl + Q to change the color of selected text </p>
<p id="log"> </p>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(document).keydown(function(e){
if(e.ctrlKey && e.keyCode == 81){
$('#log').html('you pressed Ctrl + Q');
google.script.run.setColor1();
}
});
});
</script>
</body>
</html>
希望对您有所帮助!
可以使用 modeless dialog box,而不是像 Anton Dementiev 建议的那样使用侧边栏。它可以是来自函数 onOpen() 或来自菜单项的 运行。
- 边栏上无模式对话框的优点:对话框更小,可以在屏幕上的任何地方移动(点击标题并拖动它)
- 不方便:将焦点设置在侧边栏上更容易,因为在无模式对话框中,您必须在对话框内容(可能很小)内单击才能设置焦点。
.html
与 Anton Dementiev 给出的相同,但 .gs
不同:
function onOpen() {
var ui = DocumentApp.getUi();
ui.createMenu('My Menu')
.addItem('Open Color Dialog', 'dialogBox')
.addToUi();
var htmlOutput = HtmlService // alternative to the sidebar
.createHtmlOutputFromFile('sidebar')
.setWidth(50)
.setHeight(50);
DocumentApp.getUi().showModelessDialog(htmlOutput, 'Title');
}
function dialogBox() {
var htmlOutput = HtmlService
.createHtmlOutputFromFile('sidebar')
.setWidth(50)
.setHeight(50);
DocumentApp.getUi().showModelessDialog(htmlOutput, 'Title');
}
必须在旁边的 bar/modeless 对话框中单击真的很麻烦,而且它是黑刺李,所以我用 Autohotkey 来做。
这是一个 .ahk 脚本 (source)
#IfWinActive ahk_exe chrome.exe ; the shortcut will only work on chrome
!^+p::
;PART 1: check if the docs is in full-screen (the script work with the mouse position)
; put the mouse on the right top corner of the screen, freeze the spy tool, copy past the relative position (1357, 6 ).
PixelGetColor ColorWin, 1357, 6 RGB ; get the color of this area
; In my case this part should be white in full screen
if (ColorWin!="0xFFFFFF") ; if it's white (= fullscreen is OFF)
send {f11}; then press f11 to activate fullscreen
#
PixelGetColor ColorText, 647, 86 RGB
;msgbox, 64, (%ColorText%) ; uncomment if needed for debug to get color to
; get the mouse position and the color of each one you want
if (ColorText="0x000000") { ; black
click,647, 86
click,712, 120
click, 786, 177 ; blue
}
else If (ColorText="0xFF0000") { ; blue
click,647, 86
click,712, 120
click, 767, 179 ; blue light
}
else IF (ColorText="0xE8864A") { ; blue light
click,647, 86
click,712, 120
click, 679, 176 ; red
}
else ;
{
click,647, 86
click,712, 120
click, 657, 151 ; black
}
return
我想使用键盘快捷键更改 Google 文档中所选文本的 ForegroundColor
。
我可以制作 "change the ForegroundColor" 部分(菜单项绑定到函数 setColor() ),但不能制作 "keyboard shortcut part"。
我找到了这个 code 但我很难实现它:
$(document).keydown(function(e){
//CTRL + Q keydown combo
if(e.ctrlKey && e.keyCode == 81){
$( '#output' ).html("I've been pressed!");
}
})
我的难处:
1) 我不确定将这段代码放在我的脚本编辑器中的什么位置(我尝试将它放在 onOpen()
函数中,如下所示,但也没有成功)。
2) 我不确定 $(document) 应该指的是什么。
3) 我不确定 "having to click on / activate the sidebar first for that to happen" 是什么意思。
function onOpen() {
var ui = DocumentApp.getUi();
ui.createMenu('My Menu')
.addItem('Color', 'setColor')
.addToUi();
var document = DocumentApp.getActiveDocument() // should it be here?
$(document).keydown(function(e){
//CTRL + Q keydown combo
if(e.ctrlKey && e.keyCode == 81){
SpreadsheetApp.getUi().alert('Hello, world!');
}
})
}
function setColor1() {
var selection = DocumentApp.getActiveDocument().getSelection();
if (selection) {
var elements = selection.getRangeElements();
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
// Only modify elements that can be edited as text; skip images and other non-text elements.
if (element.getElement().editAsText) {
var text = element.getElement().editAsText();
// Edit the selected part of the element, or the full element if it's completely selected.
if (element.isPartial()) {
text.setForegroundColor(element.getStartOffset(), element.getEndOffsetInclusive(), "#00FFFF");
} else {
text.setForegroundColor("#00FFFF");
}
}
}
}
}
在这一行中,变量 'document' 是 Google Apps 脚本文档中定义的 'Document' 对象的一个实例。
var document = DocumentApp.getActiveDocument()
它是对位于您云端硬盘中的 Google 文档的抽象,允许您使用此处描述的一组方法修改您的云端硬盘文件 https://developers.google.com/apps-script/reference/document/document
下面这行是jQuery语法。
$(document).keydown(function(e){}
jQuery 是一个用于客户端开发的 JavaScript 库。它用于导航网页的 HTML DOM 树。 $(document) 是 jQuery 对 DOM 的表示。更多关于 jQuery http://jquery.com/
因为 Google Apps 脚本在 Google 服务器上运行,而不是在您的本地计算机上运行,所以没有 DOM 树供您导航。您仅限于映射到 Google 服务器上的代码位的 GAS 函数。这两行代码彼此没有任何关系,也不是指同一个文档。
这同样适用于您可以收听的活动。虽然 GAS 确实具有一些事件处理功能,但事件列表非常短(参见 'Simple Triggers' 和可安装触发器')https://developers.google.com/apps-script/guides/triggers/
更新
您可以通过使用 Html 服务构建自己的自定义 UI 来实现此目的。需要注意的是,似乎无法将控制权从 Google Document 移交给 UI 元素,因此您必须在每次操作后手动将焦点设置在侧边栏上。
下面是项目中 .gs
文件的示例。将此视为您的服务器应用程序。
//creates html output from the template
function onOpen(){
var ui = DocumentApp.getUi();
var htmlOutput = HtmlService.createTemplateFromFile('sidebar').evaluate();
ui.showSidebar(htmlOutput);
}
function setColor1(){
//your code
}
下面是侧边栏模板的客户端代码。您可以通过单击文件 -> 新建 -> Html 文件在脚本编辑器中创建模板。
在上面的 .gs
文件中,将其命名为 sidebar
或您为 var htmlOutput
选择的任何名称。如果您按 'Ctrl + Q',边栏将显示确认信息并调用 .gs
文件中的 setColor1()
函数。
更多关于从客户端调用服务器端 GAS 函数的信息https://developers.google.com/apps-script/guides/html/reference/run
有关Html服务的更多信息 https://developers.google.com/apps-script/guides/html/
明显的缺点是侧边栏必须先获得焦点,所以你总是需要在选择后点击它。
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<p> Press Ctrl + Q to change the color of selected text </p>
<p id="log"> </p>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(document).keydown(function(e){
if(e.ctrlKey && e.keyCode == 81){
$('#log').html('you pressed Ctrl + Q');
google.script.run.setColor1();
}
});
});
</script>
</body>
</html>
希望对您有所帮助!
可以使用 modeless dialog box,而不是像 Anton Dementiev 建议的那样使用侧边栏。它可以是来自函数 onOpen() 或来自菜单项的 运行。
- 边栏上无模式对话框的优点:对话框更小,可以在屏幕上的任何地方移动(点击标题并拖动它)
- 不方便:将焦点设置在侧边栏上更容易,因为在无模式对话框中,您必须在对话框内容(可能很小)内单击才能设置焦点。
.html
与 Anton Dementiev 给出的相同,但 .gs
不同:
function onOpen() {
var ui = DocumentApp.getUi();
ui.createMenu('My Menu')
.addItem('Open Color Dialog', 'dialogBox')
.addToUi();
var htmlOutput = HtmlService // alternative to the sidebar
.createHtmlOutputFromFile('sidebar')
.setWidth(50)
.setHeight(50);
DocumentApp.getUi().showModelessDialog(htmlOutput, 'Title');
}
function dialogBox() {
var htmlOutput = HtmlService
.createHtmlOutputFromFile('sidebar')
.setWidth(50)
.setHeight(50);
DocumentApp.getUi().showModelessDialog(htmlOutput, 'Title');
}
必须在旁边的 bar/modeless 对话框中单击真的很麻烦,而且它是黑刺李,所以我用 Autohotkey 来做。 这是一个 .ahk 脚本 (source)
#IfWinActive ahk_exe chrome.exe ; the shortcut will only work on chrome
!^+p::
;PART 1: check if the docs is in full-screen (the script work with the mouse position)
; put the mouse on the right top corner of the screen, freeze the spy tool, copy past the relative position (1357, 6 ).
PixelGetColor ColorWin, 1357, 6 RGB ; get the color of this area
; In my case this part should be white in full screen
if (ColorWin!="0xFFFFFF") ; if it's white (= fullscreen is OFF)
send {f11}; then press f11 to activate fullscreen
#
PixelGetColor ColorText, 647, 86 RGB
;msgbox, 64, (%ColorText%) ; uncomment if needed for debug to get color to
; get the mouse position and the color of each one you want
if (ColorText="0x000000") { ; black
click,647, 86
click,712, 120
click, 786, 177 ; blue
}
else If (ColorText="0xFF0000") { ; blue
click,647, 86
click,712, 120
click, 767, 179 ; blue light
}
else IF (ColorText="0xE8864A") { ; blue light
click,647, 86
click,712, 120
click, 679, 176 ; red
}
else ;
{
click,647, 86
click,712, 120
click, 657, 151 ; black
}
return