Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert a cell in google spreadsheet using google apps script

I am working on a Google spreadsheet. I want to know if there is a way to insert a cell in the spreadsheet using apps script.

Example: Assume there are some data from A2 to Y2.

I want to insert a cell A2 with some data. So now there will be data from A2 to Z2.

like image 809
Suneel Kumar Avatar asked May 05 '14 08:05

Suneel Kumar


3 Answers

This will shift everything from A2 to the right by 1 column, leaving A2 empty and ready to insert the new data.

Do be aware that this will also copy across the formatting of A2. So make sure any borders etc. are placed on A1 and not A2.

function addNewCell() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  ss.getRange("A2").insertCells(SpreadsheetApp.Dimension.COLUMNS);
}

And if you wish to shift A2 across multiple cells, use a range

ss.getRange("A2:C2").insertCells(SpreadsheetApp.Dimension.COLUMNS);

Obviously this particular code will move them 3 across

like image 181
Mark Cooper Avatar answered Oct 13 '22 01:10

Mark Cooper


I got my answer, there is no way to insert a cell.

like image 39
Suneel Kumar Avatar answered Oct 13 '22 01:10

Suneel Kumar


There's no function to directly insert a single cell, but we can do a move and write:

  • Move range A2:Y2 to B2:Z2.
  • Write the new value to A2.

For example:

function insertCell() {
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.getRange("A2:Y2").moveTo(sheet.getRange("B2"));
  sheet.getRange("A2").setValue("New");
}
like image 22
Yang Avatar answered Oct 13 '22 01:10

Yang