Skip to content

Instantly share code, notes, and snippets.

@yuya
Created April 4, 2018 15:48
Show Gist options
  • Save yuya/4a9eda00dce4d12c48a7005456e22330 to your computer and use it in GitHub Desktop.
Save yuya/4a9eda00dce4d12c48a7005456e22330 to your computer and use it in GitHub Desktop.
[gas][sheetsapi] 選択中の行を指定した行の下に移動
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu("❖ マクロ")
.addItem("[選択行] 指定行の下に行移動", "moveRowsByActiveRow")
.addToUi()
;
}
function moveRowsByActiveRow() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var activeSheet = spreadsheet.getActiveSheet();
var currentSheetName = activeSheet.getSheetName();
var activeRangeList = SpreadsheetApp.getActiveRangeList().getRanges();
var targetRowIndex = +(Browser.inputBox("移動先の行番号を入力してください"));
var requestList = [];
var gap = 0;
if (!targetRowIndex) {
Browser.msgBox("Error: エラーが発生しました");
return;
}
activeRangeList.forEach(function (range, index) {
var startRowIndex = range.getRowIndex();
var lastRowIndex = range.getLastRow();
requestList.push({
"moveDimension": {
"source": {
"sheetId": activeSheet.getSheetId(),
"dimension": "ROWS",
"startIndex": startRowIndex - 1,
"endIndex": lastRowIndex
},
"destinationIndex": targetRowIndex + gap
}
});
gap += (lastRowIndex - startRowIndex + 1);
});
var req = {
"requests": requestList
};
Sheets.Spreadsheets.batchUpdate(JSON.stringify(req), spreadsheet.getId());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment