简体   繁体   中英

Why am I getting TypeError: Cannot read property '0' of undefined error in google apps script?

I am just trying to bold the first column data in the sheet.

function formatColumnHeader(){
var thisSheet = SpreadsheetApp.getActiveSheet();
var dataRange = thisSheet.getDataRange();
var dataValues = dataRange.getValues();

for(var row =1;row<dataValues.length;row++){
 dataRange[row][0].setFontWeight('bold');
}
}

But I get TypeError: Cannot read property '0' of undefined error. What am I doing wrong here?

You can try range.offset :

function formatColumnHeader(){
  const thisSheet = SpreadsheetApp.getActiveSheet(),
    dataRange = thisSheet.getDataRange();
  dataRange.offset(0, 0, dataRange.getNumRows(), 1).setFontWeight('bold');
}

Or if you have no need for dataRange , try getRange() to get only the range that you need:

function formatColumnHeader(){
  const thisSheet = SpreadsheetApp.getActiveSheet();
  dataRange = thisSheet.getRange(1, 1, thisSheet.getLastRow()).setFontWeight('bold');
}

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