TypeError: 无法读取 null 的属性 (读取 ‘setTabColor’)

huangapple go评论86阅读模式
英文:

TypeError: Cannot read properties of null (reading 'setTabColor')

问题

对不起,你的请求不清楚。你想让我翻译哪个部分?你可以提供具体的内容或指示,我会尽力提供帮助。

英文:

I have minimal knowledge about coding or javascript but I tried to generate a code with ChatGPT but when I try to run it in google apps script it gives me an error saying: TypeError: Cannot read properties of null (reading 'setTabColor')

The script it supposed to create a google sheets for me.
This is the script I am trying to run:

function createTaskTracker() {
  // Create a new Google Sheets document
  var sheetName = "Task Tracker";
  var spreadsheet = SpreadsheetApp.create(sheetName);

  // Set up the tabs for the document
  var tabs = ["Overall", "Work", "Social Media", "Workout", "Freetime", "Long-Term         Progress"];
  for (var i = 0; i < tabs.length; i++) {
    var tabName = tabs[i];
    var sheet = spreadsheet.insertSheet(tabName);
  }

  // Set the tab colors to match the categories
  var colors = ["#ffffff", "#00ff00", "#0000ff", "#ffff00", "#ff0000"];
  var categoryTabs = ["Work", "Sports/Training", "Freetime", "Social Media"];
  for (var i = 0; i < categoryTabs.length; i++) {
    var categoryTab = categoryTabs[i];
    var colorIndex = i + 1;
    var sheet = spreadsheet.getSheetByName(categoryTab);
    var color = colors[colorIndex];
    sheet.setTabColor(color);
  }

  // Set up the column headers for each tab
  var headers = ["Task", "Routine", "Time Spent", "Status", "Progress"];
  var tabsWithHeaders = ["Work", "Sports/Training", "Freetime", "Social Media"];
  for (var i = 0; i < tabsWithHeaders.length; i++) {
    var tabName = tabsWithHeaders[i];
    var sheet = spreadsheet.getSheetByName(tabName);
    sheet.appendRow(headers);
  }

  // Freeze the first row on each tab
  for (var i = 0; i < tabs.length; i++) {
    var tabName = tabs[i];
    var sheet = spreadsheet.getSheetByName(tabName);
    sheet.setFrozenRows(1);
  }

  // Rename the default sheet to "Settings"
  var defaultSheet = spreadsheet.getSheetByName("Sheet1");
  defaultSheet.setName("Settings");

  // Add a message to the main tab
  var mainTab = spreadsheet.getSheetByName("Overall");
  mainTab.getRange("A1").setValue("Welcome to your Task Tracker!");
}

Since I have minimal knowledge about scripts I really had no solution or didn´t find a direct answer from a google search.

答案1

得分: 1

问题在于设置标签颜色时,您有一个在当前标签/工作表名称中不存在的表格名称。

最初,标签设置如下:

var tabs = ["Overall", "Work", "Social Media", "Workout", "Freetime", "Long-Term Progress"];

要着色的标签是:

var categoryTabs = ["Work", "Sports/Training", "Freetime", "Social Media"];

由于Sports/Training不存在,当要着色它的标签时,找不到它(为null)。

请尝试这样做:

function createTaskTracker() {
    
  const Spreadsheet = SpreadsheetApp.create(`任务追踪器`)
    
  const headers = ["任务", "例行", "花费时间", "状态", "进展"]
    
  const tabs = [
    
    {
      标题: `总体`,
      标签颜色: `#ffffff`,
      有标题: false
    },
    {
      标题: `工作`,
      标签颜色: `#00ff00`,
      有标题: true
    },
    {
      标题: `社交媒体`,
      标签颜色: `#ff0000`,
      有标题: true
    },
    {
      标题: `锻炼`,
      标签颜色: `#0000ff`,
      有标题: true
    },
    {
      标题: `空闲时间`,
      标签颜色: `#ffff00`,
      有标题: true
    },
    {
      标题: `长期进展`,
      标签颜色: `#ffffff`,
      有标题: true
    },
    
  ]
    
  tabs.forEach(tab => {
    
    const newTab = Spreadsheet.insertSheet()
    newTab.setName(tab.标题)
    newTab.setTabColor(tab.标签颜色)
    
    if (tab.有标题) {
      newTab.appendRow(headers)
      newTab.setFrozenRows(1)
    }
    
  })
    
  Spreadsheet.deleteSheet(Spreadsheet.getSheetByName(`Sheet1`))
    
}
英文:

The issue is that when setting the tab colors, you have sheet name that doesn't exist in your current tabs/sheet names.

Initially, the tabs are set as:

var tabs = ["Overall", "Work", "Social Media", "Workout", "Freetime", "Long-Term Progress"];

The ones set to be colored are:

var categoryTabs = ["Work", "Sports/Training", "Freetime", "Social Media"];

Because Sports/Training doesn't exist, when it is called to have it's tab colored, it is not found (null).

Try this:

function createTaskTracker() {
const Spreadsheet = SpreadsheetApp.create(`Task Tracker`)
const headers = ["Task", "Routine", "Time Spent", "Status", "Progress"]
const tabs = [
{
Title: `Overall`,
TabColor: `#ffffff`,
HasHeaders: false
},
{
Title: `Work`,
TabColor: `#00ff00`,
HasHeaders: true
},
{
Title: `Social Media`,
TabColor: `#ff0000`,
HasHeaders: true
},
{
Title: `Workout`,
TabColor: `#0000ff`,
HasHeaders: true
},
{
Title: `Freetime`,
TabColor: `#ffff00`,
HasHeaders: true
},
{
Title: `Long-Term Progress`,
TabColor: `#ffffff`,
HasHeaders: true
},
]
tabs.forEach(tab => {
const newTab = Spreadsheet.insertSheet()
newTab.setName(tab.Title)
newTab.setTabColor(tab.TabColor)
if (tab.HasHeaders) {
newTab.appendRow(headers)
newTab.setFrozenRows(1)
}
})
Spreadsheet.deleteSheet(Spreadsheet.getSheetByName(`Sheet1`))
}

huangapple
  • 本文由 发表于 2023年3月15日 17:57:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75743098.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定