Created
November 6, 2020 22:27
-
-
Save tajuszk/e2e5b3a9ebdeb770d636781316a0d279 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sheetData = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName); | |
const titleRow = 1; // 『投稿内容』とか書いている部分の行数 | |
const startRow = 1 + titleRow; // 1行目は『投稿内容』とか書いているので2行目から | |
const startCol = 1; | |
const endRow = sheetData.getLastRow() - titleRow; // 最後の行まで(2行目から始まっているので-1している) | |
const endCol = 2; // 『投稿回数』の列までなので2列目まで | |
// ① 投稿を一括で取得する | |
const cells = sheetData.getRange(startRow, startCol, endRow, endCol).getValues(); | |
// ちなみにcellsの中身は | |
// [ [ '投稿内容', '投稿回数'] , [ '投稿内容', '投稿回数'] , [ '投稿内容', '投稿回数'] ,....,] | |
// という形式になっている | |
let postData = cells[0]; // postData = [ '投稿内容', '投稿回数'] なので postData[0] => 投稿内容, postData[1] => 投稿回数 | |
let row = 1 // 行番号(選ばれたらその行の投稿された回数を+1するために持っておく) | |
// ② 一番少ない行を探す | |
for (let i = 0, il = cells.length; i < il; i++ ) { | |
// 投稿回数が少なかったら更新(回数が同じであればそのまま) | |
if (cells[i][1] < postData[1]) { | |
postData = cells[i] | |
row = 1 + i // 行は1から始まるので+1して保存しておく | |
} | |
} | |
// ③ 投稿する内容の投稿回数の部分のセルだけ取得して、+1して更新する | |
const updateCell = sheetData.getRange(row + titleRow, 2, 1, 1); | |
updateCell.setValue(postData[1]+1); | |
// ④ 投稿内容の方を取得 | |
const postMessage = postData[0]; | |
return postMessage; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment