11
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

シートの最後の行をコピーして次の行にコピーする

Last updated at Posted at 2014-12-21

スプレッドシートを使って定期的にデータを取得していると
シートの最後の行をコピーして次の行にコピーしたいことが多くあるのでまとめておきます

{sheet}.getLastRow(); という関数があるのでこちらを使いました。

var spreadsheet = SpreadsheetApp.openById('hoge');
var sheet = spreadsheet.getSheetByName("fuga");

function copyLastRow() {
  var lastRow = sheet.getLastRow();
  var copyRow = lastRow + 1;  
  sheet.getRange(lastRow,1,1,10).copyTo(sheet.getRange(copyRow,1));
}

// コピーした後に日付(1行目)と更新する値(8行目)を変更するスクリプト
function pushWeeklyData() {
  var lastRow = sheet.getLastRow();
  var copyRow = lastRow + 1;  
  sheet.getRange(lastRow,1,1,10).copyTo(sheet.getRange(copyRow,1));
  
  var today = getToday();  
  var data  = getData('hoge');  
  sheet.getRange(copyRow,1).setValue(today);
  sheet.getRange(copyRow,8).setValue(data);
}

function getToday() {
  return Utilities.formatDate(new Date(),"JST","yyyy/MM/dd");
}
function getData(param) {
  return 'hoge!';
}
11
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?