Gists
This is a sample script for downloading a file using a button of dialog box on Google Docs (Spreadsheet, Document and Slides).
Please use this sample script at script editor on Google Docs (Spreadsheet, Document and Slides). And please set file ID in the script.
FLow :
The flow of this sample script is as follows.
- Run
dialog().
- When users click a download button, retrieve file ID at GAS side.
- Create download URL from the file ID. Download URL and filename are sent to
download(obj) of Javascript.
- Create a tag for downloading and click it at Javascript side.
- By this, users can download the file of file ID.
Code.gs
function dialog() {
var html = HtmlService.createHtmlOutputFromFile('download');
SpreadsheetApp.getUi().showModalDialog(html, 'Sample dialog'); // If you use other Google Docs, please modify here.
}
function getDownloadUrl() {
var id = "### file id ###";
var file = DriveApp.getFileById(id);
return {
url: file.getDownloadUrl().replace("?e=download&gd=true",""),
filename: file.getName()
};
}
download.html
<input type="button" value="download" onclick="getUrl()" />
<script>
function getUrl() {
google.script.run.withSuccessHandler(download).getDownloadUrl();
}
function download(obj) {
var d = document.createElement('a');
d.href = obj.url;
d.download = obj.filename;
d.click();
}
</script>
Gists
Introduction
Recently, I have introduced a GAS library and a CLI tool for rearranging files in GAS project. Those are RearrangeScripts and ggsrun. Because today, I found a new way for rearranging files, I would like to introduce it although there is a restriction.
By the recent Google-update, users become able to create folders in GAS project. The new way uses this. At first, please see the demonstration GIF animation.
Demo

Gists
This sample script is for retrieving an instance of user-interface environment for Spreadsheet, Document and Slides. When I create applications which use user interface (for example, sidebar, dialog and so on), the user interface can be used for Spreadsheet, Document and Slides. If the application doesn’t use the methods depend on Spreadsheet, Document and Slides, this script can give 3 choices to users.
function getUi() {
var ui;
try {
ui = SpreadsheetApp.getUi();
} catch(e) {}
try {
ui = DocumentApp.getUi();
} catch(e) {}
try {
ui = SlidesApp.getUi();
} catch(e) {}
return ui || null;
}
function main() {
var ui = getUi();
if (ui) {
ui.alert('Hello, world!');
}
}
Gists
This is a sample script for combining and mixing 2 objects. Each object is an array which included a dictionary type. When the key of the dictionary object is the same, the values are mixed.
This can be also used for Google Apps Script.
var obj1 = [
{"key1": ["value1a1", "value1a2"]},
{"key1": ["value1aa1", "value1aa2"]},
{"key2": ["value2a1", "value2a2"]},
{"key3": ["value3a1", "value3a2"]},
];
var obj2 = [
{"key1": ["value1b1", "value1b2"]},
{"key3": ["value3b1", "value3b2"]},
{"key3": ["value3bb1", "value3bb2"]},
{"key4": ["value4b1", "value4b2"]},
];
Output
[
{"key1": ["value1a1", "value1a2", "value1b1", "value1b2", "value1aa1", "value1aa2"]},
{"key2": ["value2a1", "value2a2"]},
{"key3": ["value3a1", "value3a2", "value3b1", "value3b2", "value3bb1", "value3bb2"]},
{"key4": ["value4b1", "value4b2"]}
]
Sample script :
Javascript :
function mixture(obj1, obj2) {
Array.prototype.push.apply(obj1, obj2);
var temp = [];
var res = [];
obj1.forEach(function(e, i){
temp[i] = !~temp.indexOf(Object.keys(e)[0]) ? Object.keys(e)[0] : false;
if (temp[i]) {
res.push(e);
} else {
res.forEach(function(f, j){
if (Object.keys(f)[0] == Object.keys(e)[0]) {
Array.prototype.push.apply(res[j][Object.keys(f)[0]], e[Object.keys(e)[0]]);
}
});
}
});
return res;
}
var obj1 = [
{"key1": ["value1a1", "value1a2"]},
{"key1": ["value1aa1", "value1aa2"]},
{"key2": ["value2a1", "value2a2"]},
{"key3": ["value3a1", "value3a2"]},
];
var obj2 = [
{"key1": ["value1b1", "value1b2"]},
{"key3": ["value3b1", "value3b2"]},
{"key3": ["value3bb1", "value3bb2"]},
{"key4": ["value4b1", "value4b2"]},
];
var res = mixture(obj1, obj2);
console.log(JSON.stringify(res))
CoffeeScript :
This is a sample script for coffeescript.
Gists
This sample script is for adding object to object by javascript.
Script :
var obj = {
key1: "value1",
key2: "value2",
key3: "value3"
};
var obj1 = {
key4: "value4",
key5: "value5",
key6: "value6"
};
Object.assign(obj, obj1);
console.log(obj);
Result :
{ key1: 'value1',
key2: 'value2',
key3: 'value3',
key4: 'value4',
key5: 'value5',
key6: 'value6' }
jsfiddle demo
Reference :
Gists
Introduction
By recent Google update (Google update at October 24, 2017), various new winds to GAS developers were blown. There is “Manifests” as one of the new winds. “Manifests” makes us manage the project using JSON. Especially, the special scopes which have to use OAuth2 process can be used by only setting them to the Manifests. I think that this is the largest modification. However, when scopes are added to a project using Manifests, users who use the project can use only added scopes. This means that when users create scripts in the project, if there are some scopes which is required to be added, such scopes cannot be automatically added. So the error of “Insufficient Permission” occurs.
Gists
This sample script is for retrieving the size (width and height) of a table in Google Slides using Google Apps Script.
There are no methods for directly retrieving the table size using SlidesApp yet. So I thought of a workaround using Slides API.
- When the slide information is retrieved using
Slides.Presentations.Pages.get() of Slides API, the information of tables is also included. In the information, the height and width of table are also included.
- But the unit is EMU (English Metric Unit), and the height and width is separated by each cell. So it is required to sum each height and width while modifying the unit.
The modified script reflected this is as follows.
Gists
By recent Google updated, Class SlideApp is added to Google Slides. SlideApp will be bring a lot of applications. Here, I would like to introduce 2 samples.
function showSidebar() {
var html = HtmlService
.createHtmlOutput('Hello, world! <input type="button" value="Close" onclick="google.script.host.close()" />')
.setTitle('My custom sidebar')
.setWidth(300);
SlidesApp.getUi().showSidebar(html);
}
2. Copy slides in existing Slide to a new Slide
This sample script create a new Slide with slides you want to copy.
Overview
This is a GAS application for rearranging Google Apps Scripts (GAS) in a project which can be seen at the script editor.
Description
Have you ever thought about rearranging Google Apps Scripts in a project which can be seen at the script editor? I also have thought about it. Finally, I could find the workaround to do it. And recently, I have given this function to ggsrun which is a CLI tool. Furthermore, I thought that if there is a GUI application for rearranging scripts in a project, it may be useful for more users. So I created this. Today, I published this as a GUI tool using Google Apps Script. If this was useful for you, I’m glad.
Overview
This is a library for zipping a folder using Google Apps Scripts.
Description
When users manually download a folder on Google Drive, users can download all files in the folder as a zip file using the web interface. There are zip tools in Class Utilities of Google Apps Script. However, the zip tools cannot create a zip file from a folder. And it cannot retrieve all files included any folders in a folder. So I created this. This library works like almost the same to the web interface using GAS.