Skip to content

Instantly share code, notes, and snippets.

@ttsukagoshi
Last active June 1, 2020 02:45
Show Gist options
  • Select an option

  • Save ttsukagoshi/ebd721b498a12e0e0227d06d14ef608a to your computer and use it in GitHub Desktop.

Select an option

Save ttsukagoshi/ebd721b498a12e0e0227d06d14ef608a to your computer and use it in GitHub Desktop.
Google App Script function to create a Google Spreadsheet in a particular Google Drive folder
/**
* Function to create a Google Spreadsheet in a particular Google Drive folder
* @param {Object} targetFolder - Google Drive folder object in which you want to place the spreadsheet
* @param {string} ssName - name of spreadsheet
* @return {string} ssId - spreadsheet ID of created spreadsheet
*/
function createSpreadsheet_(targetFolder, ssName) {
var ssId = SpreadsheetApp.create(ssName).getId();
var temp = DriveApp.getFileById(ssId);
targetFolder.addFile(temp);
DriveApp.getRootFolder().removeFile(temp);
return ssId;
}
@ttsukagoshi

ttsukagoshi commented Jun 1, 2020

Copy link
Copy Markdown
Author

A Google Docs version of this script will be:

/**
 * Function to create a Google Docs file in a particular Google Drive folder
 * @param {Object} targetFolder Google Drive folder object in which you want to place the document
 * @param {string} docName Name of document 
 * @return {string} Document ID of created spreadsheet
 */
function createDocument_(targetFolder, docName) {
  var docId = DocumentApp.create(docName).getId();
  var temp = DriveApp.getFileById(docId);
  targetFolder.addFile(temp);
  DriveApp.getRootFolder().removeFile(temp);
  return docId;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment