简体   繁体   English

为什么我会收到 TypeError: Cannot read property '0' of undefined error in google apps script?

[英]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.但是我得到 TypeError: Cannot read property '0' of undefined 错误。 What am I doing wrong here?我在这里做错了什么?

You can try range.offset :您可以尝试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:或者,如果您不需要dataRange ,请尝试getRange()以仅获取您需要的范围:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我收到 TypeError:在 Google 表格上运行应用程序脚本时无法读取未定义的属性“1”? - Why I'm getting TypeError: Cannot read property '1' of undefined while running an apps script on Google Sheets? 为什么会收到此错误类型错误:无法读取未定义的属性“客户端” - Why am getting this error TypeError: Cannot read property 'client' of undefined 我为什么会收到TypeError:无法读取未定义的属性“现在” - Why am I getting TypeError: Cannot read property 'now' of undefined 为什么我收到“ TypeError:无法读取未定义的属性” length” - Why am I getting 'TypeError: Cannot read property 'length' of undefined' 为什么我收到此错误:未捕获的TypeError:无法读取undefined的属性&#39;john&#39; - Why am I getting this error: Uncaught TypeError: cannot read property 'john' of undefined 为什么我收到错误:类型错误:无法读取未定义的属性“地图”? - Why am I getting an error : TypeError: Cannot read property 'map' of undefined? 为什么我收到错误“UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'forEach' of undefined”? - Why am I getting the error “UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'forEach' of undefined”? 我为什么会收到错误:Uncaught TypeError:无法读取未定义的属性“ left” - Why am I getting the error: Uncaught TypeError: Cannot read property 'left' of undefined 为什么我收到此错误:“未捕获的TypeError:无法读取未定义的属性'标题'”? - Why am i getting this error: “Uncaught TypeError: Cannot read property 'Title' of undefined”? 为什么我会出错? 类型错误:“无法读取未定义的属性‘执行’” - Why am I getting error? TypeError: “Cannot read property 'execute' of undefined”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM