Skip to content

搞英语 → 看世界

翻译英文优质信息和名人推特

Menu
  • 首页
  • 作者列表
  • 独立博客
  • 专业媒体
  • 名人推特
  • 邮件列表
  • 关于本站
Menu

如何从 Google 表格菜单在新窗口中打开网站

Posted on 2022-05-07

假设您已经为 Google 表格构建了一个插件,它向表格 UI 添加了一个新菜单项。您现在想在菜单中添加一个选项,单击该选项后,用户无需单击任何其他按钮即可将用户重定向到您的网站。

例如,在这个演示 Google Sheet中,我们有一个父菜单和一个在新窗口中打开基础网站的子菜单。

Google Sheets open Url

1. 在 Google 表格中添加菜单

作为第一步,我们将在 Google 表格中添加一个自定义菜单并从onOpen函数中调用它,这样当用户打开您的 Google 表格时该菜单始终可用。

 const onOpen = ( ) => { const ui = SpreadsheetApp . getUi ( ) ; const parentMenu = ui . createMenu ( '??‍? Digital Inspiration' ) ; parentMenu . addItem ( 'Visit our website' , 'openWebsite' ) ; parentMenu . addToUi ( ) ; } ;

2.为网站重定向添加HTML

在 Apps 脚本编辑器中创建一个新文件url.html并添加以下代码。

JavaScript 使用window.open方法在新窗口中打开 URL,因为我们已将目标设置为_blank 。

 <! DOCTYPE html > < html > < body > < a href = " <?= url; ?> " target = " _blank " > Click here </ a > to open the webpage. </ body > < script > var windowReference = window . open ( '<?= url; ?>' , '_blank' ) ; if ( windowReference !== null ) { google . script . host . close ( ) ; } </ script > </ html >

在弹出窗口中打开窗口

如果您想以固定大小的弹出窗口而不是新窗口打开网站,则该函数将编写为:

 < script > var windowFeatures = 'popup' ; var windowReference = window . open ( '<?= url; ?>' , 'scriptWindow' , windowFeatures ) ; if ( windowReference !== null ) { google . script . host . close ( ) ; } </ script >

如果窗口已被浏览器内置的弹出窗口阻止程序阻止,则window.open方法的返回值为 null。

弹出窗口可以放置在脚本上的任何位置,并通过修改 windowFeatures 变量调整为特定的高度和宽度,如下所示:

 // before var windowFeatures = 'popup' ; // after var windowFeatures = 'left=100,top=100,width=320,height=320' ;

有关解决与打开辅助窗口的链接相关的一些可用性问题的最佳实践,请参阅MDN 文档。

3.从谷歌表格打开链接

接下来,我们将编写将从菜单调用的 Apps 脚本函数,并在新窗口/选项卡中启动网站。

 const openWebsite = ( ) => { const htmlTemplate = HtmlService . createTemplateFromFile ( 'url.html' ) ; htmlTemplate . url = 'https://digitalinspiration.com/' ; const htmlOutput = htmlTemplate . evaluate ( ) . setHeight ( 50 ) . setWidth ( 200 ) ; const ui = SpreadsheetApp . getUi ( ) ; ui . showModelessDialog ( htmlOutput , 'Open Website' ) ; Utilities . sleep ( 2000 ) ; } ;

需要添加sleep功能,因为打开窗口可能需要一两秒钟。如果省略sleep ,则电子表格对话框将打开并立即关闭,而无需启动网站。

原文: https://www.labnol.org/open-webpage-google-sheets-220507

本站文章系自动翻译,站长会周期检查,如果有不当内容,请点此留言,非常感谢。
  • Abhinav
  • Abigail Pain
  • Adam Fortuna
  • Alberto Gallego
  • Alex Wlchan
  • Answer.AI
  • Arne Bahlo
  • Ben Carlson
  • Ben Kuhn
  • Bert Hubert
  • Bits about Money
  • Brian Krebs
  • ByteByteGo
  • Chip Huyen
  • Chips and Cheese
  • Christopher Butler
  • Colin Percival
  • Cool Infographics
  • Dan Sinker
  • David Walsh
  • Dmitry Dolzhenko
  • Dustin Curtis
  • Elad Gil
  • Ellie Huxtable
  • Ethan Marcotte
  • Exponential View
  • FAIL Blog
  • Founder Weekly
  • Geoffrey Huntley
  • Geoffrey Litt
  • Greg Mankiw
  • Henrique Dias
  • Hypercritical
  • IEEE Spectrum
  • Investment Talk
  • Jaz
  • Jeff Geerling
  • Jonas Hietala
  • Josh Comeau
  • Lenny Rachitsky
  • Liz Danzico
  • Lou Plummer
  • Luke Wroblewski
  • Matt Baer
  • Matt Stoller
  • Matthias Endler
  • Mert Bulan
  • Mostly metrics
  • News Letter
  • NextDraft
  • Non_Interactive
  • Not Boring
  • One Useful Thing
  • Phil Eaton
  • Product Market Fit
  • Readwise
  • ReedyBear
  • Robert Heaton
  • Ruben Schade
  • Sage Economics
  • Sam Altman
  • Sam Rose
  • selfh.st
  • Shtetl-Optimized
  • Simon schreibt
  • Slashdot
  • Small Good Things
  • Taylor Troesh
  • Telegram Blog
  • The Macro Compass
  • The Pomp Letter
  • thesephist
  • Thinking Deep & Wide
  • Tim Kellogg
  • Understanding AI
  • 英文媒体
  • 英文推特
  • 英文独立博客
©2025 搞英语 → 看世界 | Design: Newspaperly WordPress Theme