[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eGoogle Apps Script's Content service enables scripts to act as web services, responding to \u003ccode\u003eGET\u003c/code\u003e and \u003ccode\u003ePOST\u003c/code\u003e requests with various data formats like text, JSON, RSS, and more.\u003c/p\u003e\n"],["\u003cp\u003eThe Content service can be used to create custom RSS feeds, modifying existing feeds and adding elements like the XKCD comic's alt text for mobile viewing.\u003c/p\u003e\n"],["\u003cp\u003eScripts can be configured to provide JSON data based on user parameters, offering functionalities like checking calendar availability within specific timeframes.\u003c/p\u003e\n"],["\u003cp\u003eBy using JSONP, you can call Apps Script functions directly from a web page's JavaScript, but exercise caution as it may expose data to malicious websites.\u003c/p\u003e\n"],["\u003cp\u003eContent returned by the Content service is redirected to a one-time URL at \u003ccode\u003escript.googleusercontent.com\u003c/code\u003e, requiring HTTP clients to be configured to follow redirects for seamless data retrieval.\u003c/p\u003e\n"]]],[],null,["# Content Service\n\nWhen a script is published as a web app, the special callback functions\n`doGet()`and `doPost()` are invoked whenever a request is made to the script's\nURL. Instead of returning a user interface object created with the\n[HTML service](/apps-script/guides/html), the\n[Content service](/apps-script/reference/content) can be used to return raw\ntextual content. This allows you to write scripts that act as \"services\",\nresponding to `GET` and `POST` requests and serving data of various MIME types.\n\nThe basics\n----------\n\nHere is a simple example of the Content service: \n\n function doGet() {\n return ContentService.createTextOutput('Hello, world!');\n }\n\n[Deploy the script as a web app](/apps-script/execution_web_apps#deploying),\nusing the same steps that you would if you were serving a user interface. When\na `GET` request is made to the script's URL, the text `Hello, world!` will be\nreturned. In addition to plain text, the service also supports returning ATOM,\nCSV, iCal, JavaScript, JSON, RSS, vCard, and XML content.\n\nServing RSS feeds\n-----------------\n\nLet's try something slightly more complicated like filtering an RSS feed. The\n[XKCD](http://xkcd.com/) comics are always funny, but you can't get the full\njoke unless you hover over the comic strip to see the extra alt text.\nUnfortunately, you can't hover on a mobile browser, so this doesn't work.\n\nLet's say we wanted to edit the feed so that the extra punchline was in the feed\ndirectly, and instead of hovering you just scrolled down a bit to see it. That\nwould work fine on a mobile device. Here's the code: \n\n function doGet() {\n var feed = UrlFetchApp.fetch('http://xkcd.com/rss.xml').getContentText();\n feed = feed.replace(\n /(<img.*?alt=\"(.*?)\".*?>)/g,\n '$1' + new Array(10).join('<br />') + '$2');\n return ContentService.createTextOutput(feed)\n .setMimeType(ContentService.MimeType.RSS);\n }\n\nThat may look tricky, but it breaks down into simple parts. We use the\n[URL Fetch service](/apps-script/reference/url-fetch) to fetch the original\nXKCD RSS feed. We then use a standard JavaScript regular expression to make the\nsubstitutions we need. Finally, we wrap the edited feed in a\n[TextOutput](/apps-script/reference/content/text-output) object and set the MIME\ntype to RSS.\n\nTo see this in action, publish the script as a web app, making sure to allow\nanonymous access (since your RSS reader will be visiting it as an anonymous\nuser). Then add the URL of the service (not the original RSS feed) to your RSS\nreader or just visit it directly in a web browser. That's it!\n\nServing JSON from scripts\n-------------------------\n\nWhat else can we do with the Content service? How about serving up JSON to other\nscripts or other websites and services! Here's a simple script that implements a\nservice that anyone can use to see if a calendar slot is open at a specific\ntime. \n\n function doGet(request) {\n var events = CalendarApp.getEvents(\n new Date(Number(request.parameters.start) * 1000),\n new Date(Number(request.parameters.end) * 1000));\n var result = {\n available: events.length == 0\n };\n return ContentService.createTextOutput(JSON.stringify(result))\n .setMimeType(ContentService.MimeType.JSON);\n }\n\nAs before, publish this as an anonymous web app to make it work. In this case,\nusers of your new service can use it by adding URL parameters to the end of the\nservice URL. The `start` and `end` parameters give a time range to check,\nspecified in the standard Unix epoch. \n\n curl -L URL_OF_YOUR_SCRIPT?start=1325437200&end=1325439000\n\nThe service will return JSON that reports whether you have anything in your\ncalendar in that range. \n\n {\"available\":true}\n\nServing JSONP in web pages\n--------------------------\n\nWith a slight change, your JSON service can become\n[JSONP](http://en.wikipedia.org/wiki/JSONP), which means that it can be called\nfrom JavaScript in a browser. Here's the new script: \n\n function doGet(request) {\n var events = CalendarApp.getEvents(\n new Date(Number(request.parameters.start) * 1000),\n new Date(Number(request.parameters.end) * 1000));\n var result = {\n available: events.length == 0\n };\n return ContentService.createTextOutput(\n request.parameters.prefix + '(' + JSON.stringify(result) + ')')\n .setMimeType(ContentService.MimeType.JAVASCRIPT);\n }\n\nTo call this service from a browser, create a script tag whose `src` attribute\nis the URL of your service, with an additional parameter called `prefix`. This\nis the name of the function in your client-side JavaScript that will be called\nwith the value returned by the service. \n\n \u003cscript src=\"URL_OF_YOUR_SCRIPT?start=1325437200&end=1325439000&prefix=alert\"\u003e\u003c/script\u003e\n\nThis example will show a message box in the browser with the service output,\nsince we specify the browser's built-in `alert()` function as our prefix. The\nJavaScript code returned will look like: \n\n alert({\"available\":true})\n\n| **Warning:** Be very careful when using this JSONP technique in your scripts. Because it's possible for anyone to embed the script tag in their web page, you can be tricked into executing the script when you visit a malicious website, which can then capture the data returned. Best practice is to ensure that JSONP scripts are read-only and only return non-sensitive information.\n\nRedirects\n---------\n\nFor security reasons, content returned by the Content service isn't served from\n`script.google.com`, but instead redirected to a one-time URL at\n`script.googleusercontent.com`. This means that if you use the Content service\nto return data to another application, you must ensure that the HTTP client is\nconfigured to follow redirects. For example, in the cURL command line utility,\nadd the flag `-L`. Check the documentation for your HTTP client for more\ninformation on how to enable this behavior."]]