简体   繁体   English

如何将单个 Google Sheets 单元格的文本内容嵌入到网页中?

[英]How to embed a single Google Sheets cell's text content into a web page?

Background: I have a Google Sheet that automatically manages key organization event dates, but occasionally one has to be manually overridden.背景:我有一个 Google Sheet,它可以自动管理关键的组织活动日期,但有时必须手动覆盖。 I have been duplicating those auto calc'd event dates using PHP on the organization's web page but that doesn't handle it when the event dates are manually changed back in the Sheets database.我一直在组织的网页上使用 PHP 复制那些自动计算的事件日期,但是当在 Sheets 数据库中手动更改事件日期时,它无法处理它。

Need Statement: I wish to grab the text content of the Google "DateCalc" Sheet, cell "A33" (which contains the corrected next Date) and embed it automatically in a web page sentence that states, "The next event is scheduled for ???."需要声明:我希望获取 Google“DateCalc”表的文本内容,单元格“A33”(其中包含更正后的下一个日期)并将其自动嵌入到网页句子中,该句子指出“下一个事件安排在? ??。” where "???"在哪里 ”???” is whatever is needed for the HTML embed effort for the text content of cell A33.是对单元格 A33 的文本内容进行 HTML 嵌入工作所需的任何内容。

All the embedding approaches I have tried using the publish to web feature of Sheets, just display a frame with the entire Sheets page content, and I have not been able to find a simple single text display approach, which is what I seek.我使用 Sheets 的发布到 Web 功能尝试过的所有嵌入方法,只是显示一个包含整个 Sheets 页面内容的框架,我一直无法找到一种简单的单一文本显示方法,这正是我所寻求的。 All the solutions I have found so far are for much more complex situations.到目前为止,我找到的所有解决方案都适用于更复杂的情况。 I believe I don't need any more code on the Sheet (source) end, just an appropriate "embed code snippet" at the destination end.我相信我不需要在工作表(源)端的任何更多代码,只需要在目标端一个合适的“嵌入代码片段”。

I can embed a complete sheet but haven't found a way to embed a simple text string in line with the surrounding text in the sentence.我可以嵌入一个完整的工作表,但还没有找到一种方法来嵌入一个简单的文本字符串与句子中的周围文本一致。

This web script is the closest I have been able to come...这个网络脚本是我能来的最接近的...

 Our next meeting is on <object data="https://docs.google.com/spreadsheets/d/e/2PACX-1vQq6sXBel4SOW5U_XLnQ1xyyl4P-aK2v1R-5uofUvstMLV89_yCSqX3Lmsy4B7E21gojeG88efGFjZ0/pubhtml?gid=604484136&amp;single=true&amp;range=A29&amp;chrome=false" type="text/html" width="171" height="15"></object>.

but, if the Code Snippet really ran (I changed the file ID to protect the innocent), you would see it doesn't produce an in-line text string but an isolated "block" with the text inside it, as shown below....但是,如果 Code Snippet 真的运行了(我更改了文件 ID 以保护无辜者),您会看到它不会生成内嵌文本字符串,而是生成一个孤立的“块”,其中包含文本,如下所示。 ...

在此处输入图片说明

The text seems to be an image, not text, that is displaced above the text baseline and has a small gray blob following it that I just can't get rid of.文本似乎是图像,而不是文本,它位于文本基线上方,后面有一个我无法摆脱的小灰色斑点。

I'll assume that your server uses a combination of PHP and HTML, so one approach can be to use that PHP code to make a call to the Sheets API to retrieve the data.我假设您的服务器使用 PHP 和 HTML 的组合,因此一种方法是使用该 PHP 代码调用 Sheets API 以检索数据。 In the PHP Sheets API Quickstart you can find a example of how to set up a project to achieve a working connection between your server and the API.PHP Sheets API 快速入门中,您可以找到一个示例,说明如何设置项目以实现服务器和 API 之间的工作连接。 After you complete the tutorial, you can modify your script to include the following function so you can achieve your original request.完成本教程后,您可以修改脚本以包含以下功能,以便实现您的原始请求。

function getNextDate() {
  $client         = getClient();
  $sheetsService  = new Google_Service_Sheets($client);
  $spreadsheetID  = '{YOUR SPREADSHEET ID}';
  $cellRange      = 'DateCalc!A33';
  $serverResponse = $sheetsService->spreadsheets_values->get($spreadsheetID, $cellRange);
  $cellValue      = $serverResponse->getValues();
  if (empty($cellValue)) {
    return "NO MEETINGS AHEAD!\n";
  } else {
    foreach ($cellValue as $value) {
      return $value[0];
    }
  }
}

A totally different alternative is to use a webapp.一个完全不同的选择是使用 webapp。 You can use this tutorial to deploy a webapp that can be later embedded in your webpage to show the following meeting dates.您可以使用本教程部署一个 Web 应用程序,该应用程序稍后可以嵌入到您的网页中以显示以下会议日期。

So there are two free alternatives to accomplish your objectives: using the Sheets API or a webapp;因此,有两种免费的替代方法可以实现您的目标:使用 Sheets API 或 webapp; and in this anwser I provided a function for the API option.在这个 anwser 中,我为 API 选项提供了一个函数。 Please, don't hesitate to offer information for further clarifications or request for help.请不要犹豫,提供信息以进一步澄清或请求帮助。

Jacques- Merci beaucoup ! Jacques- Merci beaucoup !

It has been a challenging several days of non-stop effort but you put me on the right track.连续几天的努力充满挑战,但你让我走上了正确的轨道。

Using less than 20 lines of JavaScript code, including one line for the free TableTop.js library call (see https://github.com/jsoma/tabletop#if-your-publish-to-web-url-doesnt-work ) and inserting a string in the middle of my HTML sentence, yielded EXACTLY what I was seeking and the solution was INFINITELY SIMPLIER than virtually everything else I found that required multiple libraries, subscription fees, complex system calls, etc.使用不到 20 行 JavaScript 代码,包括一行用于免费 TableTop.js 库调用(请参阅https://github.com/jsoma/tabletop#if-your-publish-to-web-url-doesnt-work )并在我的 HTML 句子中间插入一个字符串,结果正是我想要的,并且解决方案比我发现的几乎所有其他需要多个库、订阅费、复杂系统调用等的东西都简单得多。

The hardest part was inserting the JavaScript code into WordPress which my web site is constructed with, as you can essentially only insert plain text into a WordPress page, but with the an added WordPress plugin (Insert Header and Footers) you can put code into a WordPress page.最难的部分是将 JavaScript 代码插入到我的网站构建的 WordPress 中,因为您基本上只能将纯文本插入到 WordPress 页面中,但是通过添加的 WordPress 插件(插入页眉和页脚),您可以将代码放入WordPress 页面。

Many thanks for getting me headed in the right direction, and having a great learning experience along the way.非常感谢让我朝着正确的方向前进,并在此过程中获得了很好的学习经验。 :-) :-)

Our next meeting is on我们的下一次会议开始 . .

I built a free, open source, and easy to use service and badge generator called https://cellshield.info .我构建了一个免费、开源且易于使用的服务和徽章生成器,名为https://cellshield.info It works out of the box for public Google spreadsheets.它开箱即用,适用于公共 Google 电子表格。 What you see above is a live Markdown output version of a badge that is synced from a spreadsheet with the value formatted as what was shown in your example with a formula of TODAY() .您在上面看到的是徽章的实时Markdown 输出版本,它是从电子表格同步的,其值的格式与您的示例中显示的格式相同,公式为TODAY() The Markdown just makes an image tag which I suppose you can do with anything else that can make an image tag as well. Markdown 只是制作了一个图像标签,我想你可以用其他任何可以制作图像标签的东西来做。

If you want the yellow or any formatting to to go away, try this:如果您希望黄色或任何格式消失,请尝试以下操作:

Our next meeting is on我们的下一次会议开始 . .

This was done by adding &color=rgba%28255%2C255%2C255%2C0.0%29&style=flat-square to the arguments.这是通过在参数中添加&color=rgba%28255%2C255%2C255%2C0.0%29&style=flat-square来完成的。 The text doesn't match perfectly, it's servicable.文字不完全匹配,它是可服务的。

Anyway, I see you went with tabletop but if you or anyone else is just looking to do what you wanted to do, then my service should be sufficient.无论如何,我看到您选择了桌面,但如果您或其他任何人只是想做您想做的事情,那么我的服务应该足够了。

For example, it is used for this Game Boy development contest to present the prize pool amount.例如,本次Game Boy开发大赛用于展示奖池金额。


If a private spreadsheet solution is needed, one could fork my code, run it on Cloud Run in their Google Cloud Platform account and explicitly share the spreadsheet to the service account on their Google Cloud Platform instance.如果需要私有电子表格解决方案,可以分叉我的代码,在他们的 Google Cloud Platform 帐户中的 Cloud Run 上运行它,并明确地将电子表格共享给他们的 Google Cloud Platform 实例上的服务帐户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM