ç®æ¬¡
GASã¨ã¯
Google Apps Scriptã®ç¥ã
Googleã«ãã£ã¦éçºãããã¹ã¯ãªãããã©ãããã©ã¼ã ã§ãã軽éã®ã¢ããªã±ã¼ã·ã§ã³éçºã®ããã®Googleã®ã¯ã¼ã¯ã¹ãã¼ã¹ãã©ãããã©ã¼ã ã
Excelã®VBAã®Googleã¹ãã¬ããã·ã¼ãçã¨è¨ãã°æ£ç¢ºã§ã¯ãªããããããããã
ContentServiceã¨HtmlServiceã¯GASã§ããã¹ããWebãã¼ã¸ãçæããããã«ä½¿ç¨ããããã®ãã®ã
GASã§Hello world!
GASã使ç¨ãããã¨ããªã人ã®ããã«ãã°ã«Hello worldã表示ãããµã³ãã«ã®ä½ææ¹æ³ãè¨è¼ããã
ã¾ã以ä¸ã®URLããApps Scriptã®ãã¼ã¸ã«ç§»åããã
移åããã以ä¸ã®ã³ã¼ããè²¼ãä»ãã¦å®è¡ããã
function myFunction() {
Logger.log('Hello world!')
}
ããã¨å®è¡ãã°ã«ãHello world!ãã¨è¡¨ç¤ºãããã
ContentServiceã§ããã¹ãããããã¤
ContentServiceã使ç¨ãã¦ä»¥ä¸ã®ããã«ã³ã¼ããæ¸ãã°ãå³ä¸ã®ãããã¤ããæ°ãããããã¤ã§GASã§è¡¨ç¤ºãããå ¬éç¨ã®URLããããã¹ãã確èªã§ããã
function doGet() {
return ContentService.createTextOutput('Hello World!')
}
ContentServiceã§JSãèªã¿è¾¼ãã§ãããã¤
UrlFetchApp.fetchã使ç¨ãã¦urlããJavaScriptãªã©ãèªã¿è¾¼ãã§ContentService.createTextOutputã§æ¸ãåºããã¨ãã§ããã
function doGet() {
const js = UrlFetchApp.fetch('https://iwb.jp/s/hello.js').getContentText()
return ContentService.createTextOutput(js)
.setMimeType(ContentService.MimeType.JAVASCRIPT)
}
setMimeTypeã追å ããã°ããã ã®ããã¹ãã§ã¯ãªãJavaScriptãªã©ã®å½¢å¼ã§è¿ããã¨ãã§ããã
è¨å®ã§ããsetMimeTypeã¯ä»¥ä¸ã®9種é¡ã
ATOM | CSV | ICAL |
JAVASCRIPT | JSON | RSS |
TEXT | VCARD | XML |
HtmlServiceã§HTMLããããã¤
åè¿°ã®setMimeTypeã«ã¯HTMLããªãã
HTMLãçæããéã¯ä»¥ä¸ã®ããã«HtmlServiceã使ç¨ããã
function doGet() {
return HtmlService.createHtmlOutput('<h1>Hello world!</h1>')
}
ç¡æã®Googleã¢ã«ã¦ã³ãã§HTMLãã¼ã¸ãçæããå ´åã¯ãã¼ã¸ä¸é¨ã«ããã®ã¢ããªã±ã¼ã·ã§ã³ã¯ãGoogle ã§ã¯ãªããå¥ã®ã¦ã¼ã¶ã¼ã«ãã£ã¦ä½æããããã®ã§ãããã¨è¡¨ç¤ºãããã
HTMLãã¡ã¤ã«ãããããã¤ãã
ãããã¤ããéã¯createHtmlOutputã§ã¯ãªãcreateTemplateFromFileã使ç¨ãã¦HTMLãã¡ã¤ã«ãèªã¿è¾¼ãã§evaluate()ãå®è¡ãã¦è¿ãã¦ãããã¤ããã»ããHTMLãã¼ã¸ãçæããããã
function doGet() {
return HtmlService
.createTemplateFromFile('index')
.evaluate()
}
HTMLãã¡ã¤ã«å ã¯<?= new Date() ?>ã®ããã«<?= ?>å ã«JavaScriptã®å¦çãå ¥ããã¨çµæãåºåã§ããã
<h1>createTemplateFromFile!</h1>
<h2><?= new Date() ?></h2>
<? ?>ã ã¨ifãªã©ãå ¥ãã¦ä»¥ä¸ã®ãããªæ¡ä»¶åå²ãã§ããã
<? if (new Date().getDay() === 0) { ?>
<h1>ä»æ¥ã¯æ¥ææ¥ã§ãã</h1>
<? } else { ?>
<h1>ä»æ¥ã¯æ¥ææ¥ã§ã¯ããã¾ããï¼</h1>
<? } ?>
Googleã¹ãã¬ããã·ã¼ãã®ãã¼ã¿ãå©ç¨
Googleã¹ãã¬ããã·ã¼ãã®ãã¼ã¿ãåå¾ãã¦ContentServiceãHtmlServiceã§ãããã¤ãããã¨ãã§ããã
function getData() {
const id = '1xdiSrYNbtpaF3J8Es3lCXAAncl6IoAvfe9JIOSvVdlY'
const ss = SpreadsheetApp.openById(id)
const lastRow = ss.getLastRow() - 1
return ss
.getSheetByName('ã·ã¼ã1')
.getRange(2, 1, lastRow, 2)
.getValues()
}
function doGet() {
const data = JSON.stringify(getData())
const output = ContentService.createTextOutput()
output.setMimeType(ContentService.MimeType.JSON)
output.setContent(data)
return output;
}
ãã¼ã¿ã®ããGoogleã¹ãã¬ããã·ã¼ã
{name: "toyota", age: 33}ã®ããã«é åã§ã¯ãªããªãã¸ã§ã¯ãã«ãããå ´åã¯ä»¥ä¸ã®ããã«ãã¦å¤æããã
function getData() {
const id = '1xdiSrYNbtpaF3J8Es3lCXAAncl6IoAvfe9JIOSvVdlY'
const ss = SpreadsheetApp.openById(id)
const lastRow = ss.getLastRow()
return ss
.getSheetByName('ã·ã¼ã1')
.getRange(1, 1, lastRow, 2)
.getValues()
}
function doGet() {
const json = getData().reduce((a, c, i, arr) => {
if (i !== 0) {
const temp = []
c.forEach((v, j) => {
temp.push(`"${arr[0][j]}":"${v}"`)
})
a.push('{' + temp.join(',') + '}')
}
return a
}, [])
const output = ContentService.createTextOutput()
output.setMimeType(ContentService.MimeType.JSON)
output.setContent('[' + json + ']')
return output
}