teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

a

2021/12/29 07:43

投稿

papinianus
papinianus

スコア12705

answer CHANGED
@@ -2,26 +2,26 @@
2
2
 
3
3
  ```javascript
4
4
  const q375824 = () => {
5
- const sheetName = `Sheet1`;
5
+ const sheetName = `シート1`;
6
- const sheet = SpreadsheetApp.getActive().getSheetByName(sheetName);
6
+ const sheet = SpreadsheetApp.getActive().getSheetByName(sheetName);
7
- const values = sheet.getDataRange().getValues();
7
+ const values = sheet.getDataRange().getValues();
8
- const colBAll = values.map(([,e])=>[e]).slice(1);
8
+ const colBAll = values.map(([,e])=>[e]).slice(1);
9
- const terminate = colBAll.findIndex(e=>e[0] === "");
9
+ const terminate = colBAll.findIndex(e=>e[0] === "");
10
- const colBs = colBAll.slice(0,terminate+1);
10
+ const colBs = terminate !== -1 ? colBAll.slice(0,terminate+1) : colBAll.slice(0);
11
- const formattedDate = Utilities.formatDate(colBs[0][0],"Asia/Tokyo","M/dd");
11
+ const formattedDate = Utilities.formatDate(colBs[0][0],"Asia/Tokyo","M/dd");
12
- const finder = sheet.createTextFinder(formattedDate);
12
+ const finder = sheet.createTextFinder(formattedDate);
13
- const matchedDates = finder.findAll();
13
+ const matchedDates = finder.findAll();
14
- if(matchedDates.length !== 2) {
14
+ if(matchedDates.length !== 2) {
15
- console.log("wrong calendar settings");
15
+ console.log("wrong calendar settings");
16
- return;
16
+ return;
17
- }
17
+ }
18
- const targetColumn = matchedDates[1].getColumn();
18
+ const targetColumn = matchedDates[1].getColumn();
19
- const colPrevious = values.map(e=>[e[targetColumn-2]]).slice(1);
19
+ const colPrevious = values.map(e=>[e[targetColumn-2]]).slice(1);
20
- if(isSame(colBs.slice(1),colPrevious.slice(1))) {
20
+ if(isSame(colBs.slice(1),colPrevious.slice(1))) {
21
- console.log("same data");
21
+ console.log("same data");
22
- return;
22
+ return;
23
- }
23
+ }
24
- sheet.getRange(2,targetColumn,colBs.length,1).setValues(colBs);
24
+ sheet.getRange(2,targetColumn,colBs.length,1).setValues(colBs);
25
25
  }
26
26
  const isSame = (a,b) => a.every((e,i)=>e[0] === b[i][0]);
27
27
  ```