Google Apps Script Images and Files Coding Examples

Google Apps Script Images and Files

File variables 1
How to create a Google Spreadsheet and give it a name, get its ID. 2
How to Create a Folder and Get the ID of the Folder 2
How to create a sheet and move the file into a specific folder 2
How to select a file and move it into a folder 3
How to add an image into a spreadsheet sheet 3
How to get an image from the web and save it to your drive 4
How to select an image file and move it into a folder 5
How to add an image from your Gdrive into a Spreadsheet sheet 6
How to make a Doc and move it to a folder 6
How to add an image from gDrive into your Document at an element index 7

File variables

const ID = ‘1QG*******FYw’; //Sheet ID

const fID = ‘1i*******Xboz’; //Folder ID

const URL = ‘http://www.discoveryvip.com/img/d.png’;

const imgID = ‘1l*******fbpJT’; // Image ID

const docID = ‘1AX*******’; //Doc ID

How to create a Google Spreadsheet and give it a name, get its ID.

​​function makerSheets() {

 const ss = SpreadsheetApp.create(‘New Sheet1’);

 Logger.log(ss.getId());

}

How to Create a Folder and Get the ID of the Folder

function makerFolder(){

 const da = DriveApp.createFolder(‘New Folder1’);

 Logger.log(da.getId());

}

How to create a sheet and move the file into a specific folder

function mover1(){

 const ss = SpreadsheetApp.create(‘New Sheet2’);

 const sid = ss.getId();

 const desDrive = DriveApp.getFolderById(fID);

 DriveApp.getFileById(sid).moveTo(desDrive);

}

How to select a file and move it into a folder

function mover(){

 const desDrive = DriveApp.getFolderById(fID);

 DriveApp.getFileById(ID).moveTo(desDrive);

}

How to add an image into a spreadsheet sheet

function adder1(){

 const ss = SpreadsheetApp.openById(ID);

 const sheet = ss.getSheets()[0];

 //sheet.appendRow([1,2,3,4,5]);

 const imageObj = UrlFetchApp.fetch(URL);

 const myData = imageObj.getContent();

 const blob = Utilities.newBlob(myData,’image/png’,’MyImage’);

 sheet.insertImage(blob,4,3);

 //Logger.log(myData);

}

How to get an image from the web and save it to your drive

function makerImage(){

 const imageObj = UrlFetchApp.fetch(URL);

 const myData = imageObj.getContent();

 const blob = Utilities.newBlob(myData,’image/png’,’MyImage’);

 const img = DriveApp.createFile(blob);

 Logger.log(img.getId());

}

How to select an image file and move it into a folder

function mover2(){

 const desDrive = DriveApp.getFolderById(fID);

 DriveApp.getFileById(imgID).moveTo(desDrive);

}

How to add an image from your Gdrive into a Spreadsheet sheet

function adder2(){

 const ss = SpreadsheetApp.openById(ID);

 const sheet = ss.getSheets()[0];

 const blob = DriveApp.getFileById(imgID).getBlob();

 sheet.insertImage(blob,10,6);

}

How to make a Doc and move it to a folder

function makerDoc(){

 const doc = DocumentApp.create(‘My Doc’);

 Logger.log(doc.getId());

 const desDrive = DriveApp.getFolderById(fID);

 DriveApp.getFileById(doc.getId()).moveTo(desDrive);

}

How to add an image from gDrive into your Document at an element index

function adderDoc(){

 const doc = DocumentApp.openById(docID);

 const blob = DriveApp.getFileById(imgID).getBlob();

 const body = doc.getBody();

 for(let i=0;i<10;i++){

  body.appendParagraph(`test${i}`);

 }

 body.insertImage(5,blob);

}