0

I would like to have a script that does the following step:

  1. The user selects a cell e.g. A5 (column A would be a unique ID column)
  2. Click a button / execute a macro shortcut, and a prompt appears asking for a position. Ex.: user enters A2
  3. Take the selected row and move it to the entered position
  4. Rename every column A value with the new value, reorder them and then move the row Ex.: The A5 value becomes the A2 value and position A5 row to A2, A2 value become the A3 value and move A2 row to A3 row, A3 value becomes A4 value and A3 row move to A4 etc...

Here's what I have so far:

function onOpen() {
  SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
      .createMenu('Custom Menu')
      .addItem('Show prompt', 'showPrompt')
      .addToUi();
}

function showPrompt() {
  var ui = SpreadsheetApp.getUi(); // Same variations.
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var value = sheet.getActiveCell().getValue();
  var range = sheet.getActiveRange().getA1Notation();

  var result = ui.prompt(
      'Move row',
      'Which position do you want:',
      ui.ButtonSet.OK_CANCEL);

  // Process the user's response.
  var button = result.getSelectedButton();
  var text = result.getResponseText();
  if (button == ui.Button.OK) {
    // User clicked "OK".
    var selectValue = sheet.getRange(text).getValue();
    var selectRange = sheet.getRange(text);
    ui.alert('range: ' + selectRange + '.');
    sheet.getRange(range).setValue(selectValue);
  } 
}

I'm at step 3, I'm trying to move the selected row to a another place with the moveTo(), but it move every row under to the selected range and move it.

sheet.getRange("A1:E").moveTo(sheet.getRange("F1"));
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Gabriel
  • 11
  • 3
  • Welcome. The selection of the start cell and the target cell is pretty easy. @Cooper write a great utility that I used/adapted many times (https://stackoverflow.com/a/45427670/1330560). What I can't follow is item#3&4. Would you please edit your question to show a detailed example of how a successful outcome might look. – Tedinoz Mar 17 '19 at 00:25
  • I've juste updated with picture, I hope it's more clear now – Gabriel Mar 19 '19 at 14:45

0 Answers0