Google文档的自定义键盘快捷键(更改颜色 – 背景颜色)

我想使用键盘快捷键更改Google文档中所选文本的ForegroundColor

我可以使“更改ForegroundColor”部分(使用绑定到函数setColor()的菜单项),而不是“键盘快捷键部分”。

我找到了这段代码但是我无法实现它:

 $(document).keydown(function(e){ //CTRL + Q keydown combo if(e.ctrlKey && e.keyCode == 81){ $( '#output' ).html("I've been pressed!"); } }) 

我的困难:

1)我不确定将此代码放在我的脚本编辑器中的位置(我试图将它放在onOpen()函数中,如下所示,但也在上面没有成功)。

2)我不确定$(文件)应该引用什么。

3)我不确定他们的意思是“必须先点击/激活侧边栏才能实现”。

 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服务器上的代码位的GASfunction。 这两行代码彼此无关,也不是指同一文档。

同样适用于您可以收听的事件。 虽然GAS确实有一些事件处理function,但事件列表很短(请参阅“简单触发器”和“可安装触发器”) https://developers.google.com/apps-script/guides/triggers/

UPDATE

您可以通过使用HtmlService构建自己的自定义UI来实现此目的。 需要注意的是,似乎无法将控制权从Google文档移交给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文件var htmlOutput sidebar或您为var htmlOutput选择的任何内容。 如果按“Ctrl + Q”,侧边栏将显示确认并调用.gs文件中的setColor1()函数。

有关从客户端调用服务器端GASfunction的更多信息,请访问https://developers.google.com/apps-script/guides/html/reference/run

有关HtmlService的更多信息https://developers.google.com/apps-script/guides/html/

明显的缺点是侧边栏必须首先获得焦点,因此您总是需要在做出选择后点击它。

       

Press Ctrl + Q to change the color of selected text

希望这可以帮助!

而不是使用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'); } 

在侧栏/无模式对话框中点击它是非常麻烦的,这很麻烦,所以我正在使用Autohotkey 。 这是一个.ahk脚本( 来源 )

 #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