tanaike - Google Apps Script, Gemini API, and Developer Tips

The Thinker

Workaround: Showing Log in Web Apps to Apps Script Dashboard using Javascript

Gists

I have already reported for showing the log to “Apps Script Dashboard” when it requests to the Web Apps. Ref In order to show the log to “Apps Script Dashboard” when it requests to the Web Apps, it is required to use the access token. But in the current stage, when the access token is used for XMLHttpRequest and fetch of Javascript in the request headers, the error related to CORS occurs. So, in this report, I would like to propose the workaround for resolving this issue.

Switching Buttons for Google Spreadsheet using Google Apps Script

Gists

These are the sample scripts for achieving the switching buttons for Google Spreadsheet using Google Apps Script. The management of images using Spreadsheet service is growing now. But, in the current stage, in order to achieve the switching buttons, it needs a little ingenuity. In this report, I would like to introduce 4 kinds of the switching buttons.

Pattern 1

In this pattern, the drawing is used as the button.

Logs in Web Apps for Google Apps Script

Gists

This is a report for retrieving the logs in Web Apps for Google Apps Script, when it requests to the Web Apps.

Experimental condition

1. Sample script for Web Apps

const doGet = (e) => {
  Logger.log(`GET method: ${JSON.stringify(e)}`);
  console.log(`GET method: ${JSON.stringify(e)}`);
  return ContentService.createTextOutput(
    JSON.stringify({ method: "GET", e: e })
  );
};
const doPost = (e) => {
  Logger.log(`POST method: ${JSON.stringify(e)}`);
  console.log(`POST method: ${JSON.stringify(e)}`);
  return ContentService.createTextOutput(
    JSON.stringify({ method: "POST", e: e })
  );
};
  • This Web Apps is deployed as Execute the app as: Me and Who has access to the app: Anyone, even anonymous.

2. Sample Google Apps Script project

  1. Google Apps Script of standalone type WITHOUT linking Google Cloud Platform (GCP) Project

Using Request Body of String JSON for Google APIs with googleapis of golang

Gists

This is a sample script for directly using the request body of the string JSON for Google APIs with googleapis of golang.

At googleapis for golang, when Google API is used, it is required to create the request body like this sample script. I have several contacts for creating about such request body. I thought that such script might be a bit difficult for users. I thought that when the string JSON object is directly used for this, it might be useful. So I would like to introduce about this. When this was useful for your situation, I’m glad.

Converting SVG Format to PNG Format using Google Apps Script

Gists

This is a sample script for converting the SVG image data to PNG image data using Google Apps Script.

Unfortunately, in the current stage, there are no methods for directly converting the SVG to PNG in Google Drive service. But it can be achieved by Drive API. The sample script is as follows.

Before you use this, please enable Drive API at Advanced Google services.

Sample script

function myFunction() {
  const svgFileId = "###"; // Please set the fileId of the SVG file.

  const url = Drive.Files.get(svgFileId).thumbnailLink.replace(
    "=s220",
    "=s1000"
  );
  const blob = UrlFetchApp.fetch(url).getBlob(); // blob is the image blob of PNG format.

  // In this sample, the retrieved image blob is put to Spreadsheet.
  const sheet = SpreadsheetApp.openById("###").getSheetByName("Sheet1");
  sheet.insertImage(blob, 1, 1).setWidth(500).setHeight(500);
}
  • In this sample script, the converted PNG image is put to the Spreadsheet.

Setting Number Format of Cells on Google Spreadsheet using batchUpdate in Sheets API with golang

Gists

This is a sample script for setting the number format of cells on Google Spreadsheet using batchUpdate in Sheets API with golang. In this case, googleapis for golang is used. The script of the authorization can be seen at the official document.

Sample script

In this script, the number format of the column “A” is changed to yyyy-mm-dd hh:mm:ss. And, please include https://www.googleapis.com/auth/spreadsheets to the scopes.

sheetId := 12345678  // Please set the sheet ID which is not Spreadsheet ID. Please be careful this.

repeatCellRequest := &sheets.RepeatCellRequest{
    Fields: "userEnteredFormat.numberFormat",
    Range: &sheets.GridRange{
        SheetId:          int64(sheetId),
        StartRowIndex:    0,
        StartColumnIndex: 0,
        EndColumnIndex:   1,
    },
    Cell: &sheets.CellData{
        UserEnteredFormat: &sheets.CellFormat{
            NumberFormat: &sheets.NumberFormat{
                Pattern: "yyyy-mm-dd hh:mm:ss",
                Type:    "DATE",
            },
        },
    },
}
requestBody := &sheets.BatchUpdateSpreadsheetRequest{
    Requests: []*sheets.Request{&sheets.Request{
        RepeatCell: repeatCellRequest,
    }},
}
resp, err := srv.Spreadsheets.BatchUpdate(bookID, requestBody).Do()
if err != nil {
    log.Fatal(err)
}
fmt.Printf("%#v\n", resp)

References

Statistics of triggers Tag on Stackoverflow

Gists

This is the statistics for the tag triggers on Stackoverflow. The tag of triggers was created at 2011-07-28. This statistics are retrieve from Stackoverflow using Stackexchange API. About 2020, the data is retrieved from 2020-01-01 to 2020-07-16.

This statistics data was obtained at 2020-07-16.

Measurement result

Fig. 1: About triggers tag. Year vs. Total questions, Answered, Solved and Closed questions



Fig. 2: About google-apps-script tag and triggers tag. Year vs. Total questions, Answered, Solved and Closed questions

Retrieving Users, Sessions and PageViews of User Summary Report from Google Analytics using Google Apps Script

Gists

This is a sample script for retrieving “Users”, “Sessions” and “PageViews” of User Summary Report from Google Analytics using Google Apps Script. When you use this, please enable Analytics Reporting API at Advanced Google services.

Sample script

function myFunction() {
  const viewId = "###";
  const startDate = "2020-01-01";
  const endDate = "2020-06-01";

  const resource = {
    reportRequests: [
      {
        viewId: viewId,
        dateRanges: [{ startDate: startDate, endDate: endDate }],
        metrics: [
          { expression: "ga:users" },
          { expression: "ga:sessions" },
          { expression: "ga:pageviews" },
        ],
      },
    ],
  };
  const res = AnalyticsReporting.Reports.batchGet(resource, { fields: "*" });
  console.log(res);
}

Result

{
  "reports": [
    {
      "columnHeader": {
        "metricHeader": {
          "metricHeaderEntries": [
            {
              "name": "ga:users",
              "type": "INTEGER"
            },
            {
              "type": "INTEGER",
              "name": "ga:sessions"
            },
            {
              "type": "INTEGER",
              "name": "ga:pageviews"
            }
          ]
        }
      },
      "data": {
        "maximums": [
          {
            "values": ["###", "###", "###"]
          }
        ],
        "rowCount": 1,
        "totals": [
          {
            "values": ["###", "###", "###"]
          }
        ],
        "minimums": [
          {
            "values": ["###", "###", "###"]
          }
        ],
        "isDataGolden": true,
        "rows": [
          {
            "metrics": [
              {
                "values": ["###", "###", "###"]
              }
            ]
          }
        ]
      }
    }
  ]
}

Reference

Tags Using Together with google-apps-script Tag at Stackoverflow

Gists

This table shows all tags using together with the tag of “google-apps-script” at Stackoverflow. The tags are summarized every year. At 2020, the data is retrieved from January 1, 2020 to July 16, 2020. The number enclosed (###) means the number of use of the tag. When this table was a material for discussing about the current tags for “google-apps-script”, I’m glad.

Year Used Tag list
2008
2009 google-form(1),google-sheets(1),google-apps-script(1)
2010 google-apps-script(50),google-sheets(31),javascript(23),google-apps(4),custom-function(2),google-calendar-api(2),spreadsheet(2),google-docs-api(2),vba(2),google-docs(2),amazon(1),amazon-web-services(1),api(1),timezone(1),ms-office(1),xml-namespaces(1),xml(1),closures(1),google-sites(1),google-maps-api-3(1),array-formulas(1),google-sheets-formula(1),google-data-api(1),date-manipulation(1),datetime(1)
2011 google-apps-script(72),google-sheets(31),google-apps(16),javascript(15),google-docs(8),google-spreadsheet-api(4),google-form(3),google-api(3),google-docs-api(2),spreadsheet(2),google-fusion-tables(2),google-calendar-api(2),java(2),google-app-engine(2),number-formatting(1),file-upload(1),google-apps-marketplace(1),json(1),email(1),html(1),tabpanel(1),gmail(1),performance(1),user-interface(1),zend-framework(1)
2012 google-apps-script(1877),google-sheets(345),javascript(161),google-docs(63),google-sites(61),google-apps(54),google-spreadsheet-api(44),google-drive-api(44),gmail(40),google-calendar-api(31),google-docs-api(29),html(19),google-visualization(18),triggers(18),email(15),google-form(15),spreadsheet(14),urlfetch(13),gwt(13),oauth(13),custom-function(13),google-app-engine(12),user-interface(11),jquery(11),jdbc(10)
2013 google-apps-script(2606),google-sheets(765),javascript(413),google-drive-api(124),google-docs(120),google-form(97),google-spreadsheet-api(78),google-apps(77),gmail(58),google-sites(57),html(56),google-calendar-api(41),google-api(34),email(31),google-docs-api(26),jquery(24),arrays(23),triggers(22),forms(22),google-app-engine(21),google-visualization(21),user-interface(20),spreadsheet(20),json(19),google-caja(19)
2014 google-apps-script(2758),google-sheets(875),javascript(606),google-form(177),google-drive-api(168),google-docs(143),google-spreadsheet-api(90),google-apps(84),html(81),google-calendar-api(61),google-sites(56),gmail(54),email(53),jquery(49),arrays(45),forms(39),spreadsheet(35),triggers(35),excel(33),json(33),google-docs-api(28),google-api(28),google-app-engine(24),google-visualization(22),google-bigquery(22)
2015 google-apps-script(2781),google-sheets(1134),javascript(714),google-form(159),html(158),google-docs(127),google-apps(115),google-spreadsheet-api(98),google-drive-api(97),gmail(66),arrays(57),google-calendar-api(57),jquery(51),google-api(46),forms(40),spreadsheet(39),email(38),google-docs-api(36),json(35),triggers(33),google-sites(32),regex(32),google-visualization(30),php(23),css(20)
2016 google-apps-script(2888),google-sheets(1198),javascript(783),google-form(156),html(118),google-drive-api(117),google-apps(117),google-docs(77),arrays(74),google-spreadsheet-api(73),json(60),gmail(59),email(46),google-calendar-api(43),google-sheets-api(42),google-api(42),triggers(38),google-visualization(31),jquery(31),spreadsheet(31),google-docs-api(29),date(24),regex(23),forms(22),csv(22)
2017 google-apps-script(3873),google-sheets(1537),javascript(979),google-form(194),html(158),google-sheets-api(137),google-docs(127),google-apps(122),google-drive-api(122),google-spreadsheet-api(114),arrays(110),gmail(85),email(64),json(56),gmail-api(52),google-api(52),triggers(51),jquery(51),spreadsheet(48),google-calendar-api(42),regex(34),google-sites(31),forms(27),custom-function(27),excel(27)
2018 google-apps-script(4716),google-sheets(2062),javascript(1046),google-form(189),google-drive-api(189),google-docs(167),google-sheets-api(165),gmail-addons(155),html(134),triggers(129),gmail(120),arrays(111),google-calendar-api(79),json(68),google-app-maker(63),email(59),google-apps-script-addon(55),gmail-api(54),google-api(50),regex(50),custom-function(48),gsuite(47),google-bigquery(45),google-slides-api(43),web-applications(43)
2019 google-apps-script(6731),google-sheets(3300),javascript(1352),google-form(263),html(250),google-sheets-api(223),google-drive-api(216),google-sheets-formula(168),google-docs(158),gmail(154),web-applications(143),arrays(141),google-sheets-macros(134),triggers(128),google-apps-script-web-application(112),google-calendar-api(88),gmail-addons(85),google-apps-script-addon(85),json(82),google-apps(77),gsuite(76),google-api(69),google-app-maker(64),scripting(61),google-apps-script-editor(61)
2020 google-apps-script(4853),google-sheets(2503),javascript(1221),html(199),google-form(176),google-drive-api(161),google-apps-script-web-application(160),arrays(154),google-sheets-api(149),google-docs(124),google-sheets-formula(120),google-sheets-macros(90),gmail(86),json(85),google-apps-script-simple-triggers(69),google-calendar-api(68),urlfetch(66),google-apps-script-addon(65),spreadsheet(57),triggers(51),gsuite-addons(48),gs-installable-triggers(47),email(47),gsuite(47),google-apps-script-editor(45)

Sample Scripts for Requesting to Web Apps by Various Languages

Gists

These are the sample scripts by the various languages for requesting to Web Apps created by Google Apps Script.

  1. curl
  2. Google Apps Script
  3. Javascript
  4. ajax
  5. Node.js
  6. axios
  7. angular
  8. go
  9. python
  10. php
  11. powershell

Sample script for Web Apps

  • Sample script for Web Apps is as follows.

    const doGet = (e) =>
      ContentService.createTextOutput(
        JSON.stringify({ method: "GET", eventObject: e })
      ).setMimeType(ContentService.MimeType.JSON);
    const doPost = (e) =>
      ContentService.createTextOutput(
        JSON.stringify({ method: "POST", eventObject: e })
      ).setMimeType(ContentService.MimeType.JSON);
    
  • Setting for Web Apps is as follows.