简体   繁体   中英

Change Row Alignment Based on Cell Value - Google Scripts

the following script is supposed to change the row alignment in a Google sheet to 'right' if the value of column E in the row is 'Post.' When I run the script it does not pop any errors, however, it does not do anything either.

Can anyone take a look at the below and provide some guidance?

Thanks!

function rowLoop() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("KOD Tests");
  var endRow = ss.getLastRow();

  for (var r = 1; r < endRow; r++) {
    rowAlignment(r);
    }
}

function rowAlignment(r) {
  var sheet = SpreadsheetApp.getActiveSheet();
  var c = sheet.getLastColumn();
  var dataRange = sheet.getRange(r, 1, 1, c);

  var data = dataRange.getValue();
  var row = data[4];

  if(row === 'Post') {
    data.setHorizontalAlignment('right');
  }
  SpreadsheetApp.flush();
}

Here it is with a few things fixed (please see comments)

function rowLoop() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  var endRow = ss.getLastRow();
  // <= to repeat for all rows
  for (var r = 1; r <= endRow; r++) {
    rowAlignment(r);
    }
}

function rowAlignment(r) {
  var sheet = SpreadsheetApp.getActiveSheet();
  var c = sheet.getLastColumn();
  var row = sheet.getRange(r, 1, 1, c);
  // Get cell in column E of row
  var cell=row.getCell(1,5);
  // Get its value
  var data = cell.getValue();
  // Test equal to 'Post' with ==
  if(data == 'Post') {
    row.setHorizontalAlignment('right');
  }
  SpreadsheetApp.flush();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM