Upgrade to Pro — share decks privately, control downloads, hide ads and more …

My GAS Tips

noviiro
February 05, 2022

My GAS Tips

noviiro

February 05, 2022
Tweet

More Decks by noviiro

Other Decks in Programming

Transcript

  1. My GAS Tips • Google Forms x </Details> = ؆୯ܝࣔ൘

    • GoogleϑΥʔϜͷճ౴ΛγʔτͰ؅ཧ͍ͯ͠ Δ৔߹ͷσʔλൺֱʢಉ࣌ʹফ͍ͨ͠౳ʣ͸ ϛϦඵͰ
  2. GASͰ࡞ΔHtmlαϯϓϧ function doGet() { return HtmlService .createTemplateFromFile('Index') .evaluate(); } function

    getData() { return SpreadsheetApp .openById('1234567890abcdefghijk lmnopqrstuvwxyz') .getActiveSheet() .getDataRange() .getValues(); } <!DOCTYPE html> <html> <head> <base target="_top"> </head> <body> <? var data = getData(); ?> <table> <? for (var i = 0; i < data.length; i+ +) { ?> <tr> <? for (var j = 0; j < data[i].length; j++) { ?> <td><?= data[i][j] ?></td> <? } ?> </tr> <? } ?> </table> </body> </html> IUUQTEFWFMPQFSTHPPHMFDPNBQQTTDSJQUHVJEFTIUNMUFNQMBUFTJOEFYIUNM@
  3. Htmlίʔυαϯϓϧ <!DOCTYPE html> <html> <head> <base target="_top"> </head> <body> <?

    let data = getData(); ?> <div> <? let iMax = data.length, j = iMax, i = 1; ?> <? for(i;i < j;i++){ let n = iMax - i; let postDate = new Date(data[n][0]).toLocaleString('ja-JP',{ timeZone: "Asia/ Tokyo" }) ?> <details open> <summary> <?= data[n][1] ?> <?= postDate ?> </summary> <pre><?= data[0][2] ?>&#009;<?= data[n][2] ?></pre> <pre><?= data[0][3] ?>&#009;<?= data[n][3] ?></pre> <pre><?= data[0][4] ?>&#009;<?= data[n][4] ?></pre> </details> <? } ?> </div> </body> </html>
  4. ϑΥʔϜͷճ౴ΛγʔτͰ؅ཧ ͍ͯ͠Δ࣌ͷσʔλൺֱ • ྫ͑͹ɿGoogleϑΥʔϜ͔Βճ౴👉εϓϨουγʔτʹσʔλ஝ੵ👉 γʔτͷApps ScriptͰճ౴ҰཡϖʔδΛGASͰදࣔͯ͠ΔΜ͚ͩͲɺͦ ͷϖʔδͰฤू࡟আ͍ͤͨ͞ʂ • GoogleϑΥʔϜͷճ౴೔࣌ΛGASͰऔಘ͢Δ࣌ʹλΠϜελϯϓҎ֎ͷ औΓํ͕ͳ͍ʁ(ࢀরɿClass

    FormResponseʣ • εϓϨουγʔτଆ͸ɺϩʔΧϥΠζ͞ΕͨλΠϜελϯϓ͕ه࿥͞ΕΔ • →ϑΥʔϜ΋εϓϨουγʔτ΋ɺϛϦඵʹม׵͢Ε͹ಉͩ͡͠ಉҰੑ͸ ͋Δఔ౓୲อͰ͖ΔʢΑΓݫີʹ͢ΔͳΒϝʔϧΞυϨεͰͷൺֱ΋௥ Ճʣ
  5. GoogleϑΥʔϜ͔Βͷऔಘྫ function myFunction() { const formId = "target form id";

    // ID͔ΒϑΥʔϜσʔλΛऔಘ͠ɺ֤ճ౴ͷλΠϜελϯϓͱ಺༰Λϩάग़ྗ let form = FormApp.openById(formId); let formResponses = form.getResponses(); for (let i = 0; i < formResponses.length; i++) { let formResponse = formResponses[i]; // ֤ճ౴ͷλΠϜελϯϓΛऔಘ͠ɺLogger΁ग़ྗ let timestamp = new Date(formResponses[i].getTimestamp()).getTime().toString(); Logger.log('Response #%s timestamp was %s', (i + 1).toString(), timestamp); let itemResponses = formResponse.getItemResponses(); // ֤ճ౴಺༰ΛLogger΁ग़ྗ for (let j = 0; j < itemResponses.length; j++) { let itemResponse = itemResponses[j]; Logger.log('Response #%s to the question "%s" was "%s"', (i + 1).toString(), itemResponse.getItem().getTitle(), itemResponse.getResponse()); } } }