17

Google Spreadsheet doesn't have the functionality to "insert cut cells" like in Excel.

Let's say I selected A4 to B5, hit Ctrl+X.
Then I moved the selection to A2.
Now I want to "insert cut cells", probably by inserting blank cells and moving the dotted-range to A2.
screenshot

Is there any way to do it using JavaScript on your own menu?

eg.

function insertCutOrCopiedCells(){
  var SS = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = SS.getActiveSheet();
  var cell = sheet.getActiveCell();

  // How do you get the cells being cut/copied? (ie. A4 to B5)

  // We can then insert empty cells (2 cols 2 rows) at the selection (A2)
  // and move the cut cells (A4 to B5) there
}
Aximili
  • 28,626
  • 56
  • 157
  • 216
  • Apps script can't listen to keyboard nor check the clipboard. – Kriggs Apr 30 '15 at 02:26
  • Yeah, but it's not only on clipboard. It's marked with dotted border on the sheet. So I was just wondering if there was a way to get that range. – Aximili Apr 30 '15 at 05:54
  • 5
    Google Sheets does have to ability to move rows (or columns), the equivalent of cut and insert. Select the *whole* rows, move your cursor over the row **number**, cursor changes into a hand, and drag. – Nigel Touch Apr 17 '16 at 12:12

3 Answers3

22

To achieve the insert cut cells feature in google sheets you simply use drag and drop. First highlight the row(s) you want to move then mouse over the row number (you will see a hand icon); then, holding your left-click, and keeping your mouse over the row numbers, you will see a dark line where the insert will take place. Let go of your mouse button and voila.

enter image description here

Matthew Lock
  • 13,144
  • 12
  • 92
  • 130
Mike
  • 329
  • 2
  • 4
  • 1
    @TT Hold CTRL before your release the left-click – MagTun Sep 30 '17 at 11:22
  • @Enora, Wow. Thanks for making me take a second look at this one. **Holding CTRL and dragging the selection** works! I also re-read this answer and found it works as well - the key is to **grab the selection from the row area**. Thank you! – TT-- Sep 30 '17 at 14:52
  • @TT by the way, holding CTRL works everywhere: try it in a text editor like notepad, or in file explorer to copy a file/folder – MagTun Sep 30 '17 at 16:56
  • 4
    am I going crazy, or did the hold CTRL just recently **stop** working !? – TT-- Oct 05 '17 at 19:28
  • Excellent. I was going crazy until I found this. BTW I didn't need to hold CTRL down – Matthew Lock Oct 06 '17 at 09:40
  • 1
    Is there any way to do this using the keyboard ? In Excel, this would be as simple as Ctrl+X then Ctrl + '+' – Alexandre d'Entraigues Nov 06 '17 at 18:24
  • 4
    This answer doesn't really address the original question, though it's a good one to know about. You can't always use drag and drop very easily, e.g. when rows are on a different sheet, and when they're very far apart or rows are very high it's difficult to drag to the desired position. – JD Smith May 15 '18 at 16:43
  • It's a shame that this stopped working somewhere along the way: It _definitely_ does not work on Mac OS X in 2019. – Dave Land Sep 13 '19 at 20:59
  • This moves the rows. How do you copy them? – Elliott B Oct 08 '19 at 20:53
  • Ditto (for above): I do not yet see a process that copies the rows (or columns) instead of moving them. Is there some process that copies? – Johnny Utahh Sep 27 '20 at 22:36
2

You have both methods, check'em in the class Range.

moveTo(target)

Cut and paste (both format and values) from this range to the target range.

copyTo(destination)

Copies the data from a range of cells to another range of cells. Both the values and formatting are copied.

Edit:

To complete the function you'll have to use also:

insertRowsAfter(afterPosition, howMany) in class Spreadsheet

Inserts a number of rows after the given row position.

and

getActiveRange() with getRow() to check where's the selection at:

Returns the range of cells that is currently considered active. This generally means the range that a user has selected in the active sheet, but in a custom function it refers to the cell being actively recalculated.

Since you don't have direct acess to the clipboard, you'll have to set up a sidebar, or a modelessDialog, which asks for a range to copy from, and it would paste into the selected area, or the other way around, paste the current selected area onto an inputed ROW.

Kriggs
  • 3,731
  • 1
  • 15
  • 23
  • Thanks Kriggs. I think the problem is still: How do you get the cells being cut? I added an example above. Thanks! – Aximili Apr 30 '15 at 01:57
1

I think , you can separate the function.

Copy : use getRange with getValue: link => https://developers.google.com/apps-script/reference/spreadsheet/sheet

Delete data : use the getRange with setValue = blank

example :

var Spreadsheet=
 SpreadsheetApp.openById(SPREADSHEET_ID).getSheetByName("number1"); 
 var value = Spreadsheet.getRange(1,0).getValue();  // copy  
 Spreadsheet.getRange(1,1).setValues(value); // insert
 Spreadsheet.getRange(1,0).setValues("");

You can use the metho copyTo

example :

var sheet = SpreadsheetApp.getActiveSheet();
 sheet.getRange("A:E").copyTo(sheet.getRange("F1"), {contentsOnly:true});
 }
Hann
  • 727
  • 3
  • 11
  • 22
  • Thanks Hann. But how do you get the selected range being cut/copied? Please see the updated question I added an example. – Aximili Apr 30 '15 at 01:55