Google App Script - SyntaxError: Unexpected token , in JSON at position 1 - When Mapping Background Color Array
Google App Script - SyntaxError: Unexpected token , in JSON at position 1 - When Mapping Background Color Array
我一直在尝试将背景颜色批量更新回跨页sheet。社区在帮助我 。最后,我运行报错,提示我post作为新题。
当前代码:
var TestArray = Sheets.Spreadsheets.get("1pcIKNUFmkk0d-UGg1sXl5xbsJC2WhocIHpM3et-CMgo", {
ranges:"TestBackgroundSheet!A1:AD39",
fields:"sheets(data(rowData(values(effectiveFormat.backgroundColor))))"
});
for (var x = 0; x < 40; x++) {
Logger.log(x + JSON.stringify(TestArray["sheets"][0]["data"][0]["rowData"][x]));
}
var rowData = TestArray["sheets"][0]["data"][0]["rowData"]
.map(row => row.getValues()).toString()
var backgroundColors = JSON.parse("[" + rowData + "]")
.map(value => {
let v = value["effectiveFormat"]
return v ? v["backgroundColor"] : null
})
这returns一个错误如下SyntaxError: Unexpected token , in JSON at position 1 (line 184, file "macros")
。其中第 184 行是 var backgroundColors = JSON.parse("[" + rowData + "]")
。 Logger 语句结果的前几行在下面 posted:
0{}
[20-10-21 18:05:08:257 PDT] 1{"values":[{},{"effectiveFormat":{"backgroundColor":{"red":1,"blue":1,"green":1}}}]}
[20-10-21 18:05:08:260 PDT] 2{}
[20-10-21 18:05:08:262 PDT] 3{}
[20-10-21 18:05:08:265 PDT] 4{"values":[{"effectiveFormat":{"backgroundColor":{"red":1,"blue":1,"green":1}}},{"effectiveFormat":{"backgroundColor":{"blue":1,"green":1,"red":1}}},{},{"effectiveFormat":{"backgroundColor":{"green":1,"red":1,"blue":1}}},{"effectiveFormat":{"backgroundColor":{"blue":1,"red":1,"green":1}}},{},{"effectiveFormat":{"backgroundColor":{"green":1,"red":1,"blue":1}}},{"effectiveFormat":{"backgroundColor":{"red":1,"blue":1,"green":1}}},{},{"effectiveFormat":{"backgroundColor":{"green":1,"blue":1,"red":1}}},{"effectiveFormat":{"backgroundColor":{"blue":1,"green":1,"red":1}}},{},{"effectiveFormat":{"backgroundColor":{"green":1,"blue":1,"red":1}}},{"effectiveFormat":{"backgroundColor":{"blue":1,"red":1,"green":1}}},{},{"effectiveFormat":{"backgroundColor":{"green":1,"red":1,"blue":1}}},{"effectiveFormat":{"backgroundColor":{"green":1,"red":1,"blue":1}}},{},{"effectiveFormat":{"backgroundColor":{"green":1,"blue":1,"red":1}}},{"effectiveFormat":{"backgroundColor":{"red":1,"green":1,"blue":1}}},{},{"effectiveFormat":{"backgroundColor":{"red":1,"green":1,"blue":1}}},{"effectiveFormat":{"backgroundColor":{"blue":1,"red":1,"green":1}}},{},{"effectiveFormat":{"backgroundColor":{"green":1,"red":1,"blue":1}}},{"effectiveFormat":{"backgroundColor":{"green":1,"blue":1,"red":1}}},{},{"effectiveFormat":{"backgroundColor":{"red":1,"green":1,"blue":1}}},{"effectiveFormat":{"backgroundColor":{"red":1,"green":1,"blue":1}}}]}
正如我所见,代码的第一行是空的,我不确定为什么它暗示位置 1 中有一个意想不到的“,”。将不胜感激关于如何正确映射它的建议。
编辑#1:
我的总体目标是提取一个包含完整 sheets 背景颜色的数组,然后对该数组进行调整,然后将其批量更新回相同的 sheet。在社区的指导下,我得到了一个简化但实用的版本。此代码如下。
function myFunction() {
var TestArray = Sheets.Spreadsheets.get("1eAq-RbtrCSMRPZ0p7XIpG3vd29yL-3SQ3D3JGyiUhKg", {
ranges:"Awesome!A1:C3",
fields:"sheets(data(rowData(values(effectiveFormat.backgroundColor))))"
});
var backgroundColors = TestArray["sheets"][0]["data"][0]["rowData"]
.map(row => row["values"]
.map(value => value["effectiveFormat"]["backgroundColor"]));
var TotalText = Sheets.Spreadsheets.Values.get("1eAq-RbtrCSMRPZ0p7XIpG3vd29yL-3SQ3D3JGyiUhKg", "Awesome!A1:C3").values;
//Map Background Colors
var colorrows = backgroundColors.map(rowColors => {
return {
values: rowColors.map(cellColor => {
return {
userEnteredFormat: {
backgroundColor: cellColor
}
}
})
}
})
var spreadsheetId = "1eAq-RbtrCSMRPZ0p7XIpG3vd29yL-3SQ3D3JGyiUhKg";
var result = Sheets.Spreadsheets.batchUpdate({
requests: [{
updateCells: {
rows: colorrows,
fields: 'userEnteredFormat.backgroundColor',
start: {
sheetId: 1616717220,
rowIndex: 0,
columnIndex: 0
}
}
}]
}, spreadsheetId)
}
现在,当我尝试在更复杂的传播中使用此代码时sheet,我 运行 遇到了一些错误(通常是因为单元格为空),这使我遇到了错误 SyntaxError: Unexpected token , in JSON at position 1 (line 184, file "macros")
来自这个问题。
你现在已经修复了那个错误(我非常感谢)并给我留下了一个带有输出的数组。
[
{"rowNumber":1,"columnNumber":1,"backgroundColor":{"blue":1,"red":1,"green":1}},
,
,
,
]
原始的简化但功能测试 sheet 将输出一个数组,该数组将被输入到 batchupdate 代码中。此函数的格式如下。
[20-10-23 11:32:04:093 EDT] {values=[{userEnteredFormat={backgroundColor={red=1.0}}}, {userEnteredFormat={backgroundColor={green=1.0, red=1.0, blue=1.0}}}, {userEnteredFormat={backgroundColor={red=1.0, blue=1.0, green=1.0}}}]}
[20-10-23 11:32:04:098 EDT] {values=[{userEnteredFormat={backgroundColor={red=1.0, blue=1.0, green=1.0}}}, {userEnteredFormat={backgroundColor={green=1.0, red=1.0}}}, {userEnteredFormat={backgroundColor={green=1.0, blue=1.0, red=1.0}}}]}
[20-10-23 11:32:04:102 EDT] {values=[{userEnteredFormat={backgroundColor={blue=1.0, green=1.0, red=1.0}}}, {userEnteredFormat={backgroundColor={red=1.0, blue=1.0, green=1.0}}}, {userEnteredFormat={backgroundColor={red=0.2901961, green=0.5254902, blue=0.9098039}}}]}
我想知道您的代码 -> {"rowNumber":1,"columnNumber":1,"backgroundColor":{"blue":1,"red":1,"green":1}},
的输出是否需要转换为 -> {values=[{userEnteredFormat={backgroundColor={blue=1.0, green=1.0, red=1.0}}}
格式,然后再放回此代码中。
var spreadsheetId = "1eAq-RbtrCSMRPZ0p7XIpG3vd29yL-3SQ3D3JGyiUhKg";
var result = Sheets.Spreadsheets.batchUpdate({
requests: [{
updateCells: {
rows: colorrows,
fields: 'userEnteredFormat.backgroundColor',
start: {
sheetId: 1616717220,
rowIndex: 0,
columnIndex: 0
}
}
}]
}, spreadsheetId)
问题 1 的答案:
修改点:
- 在你的脚本中,我认为需要检查
rowData
的数组中是否有values
的属性,以及是否有[ values
数组中 effectiveFormat
的 =81=]。
- 我认为这就是您出错的原因。
当这些点反映到你的脚本中,就变成了下面这样。
修改后的脚本:
var TestArray = Sheets.Spreadsheets.get("1pcIKNUFmkk0d-UGg1sXl5xbsJC2WhocIHpM3et-CMgo", {
ranges:"TestBackgroundSheet!A1:AD39",
fields:"sheets(data(rowData(values(effectiveFormat.backgroundColor))))"
});
// for (var x = 0; x < 40; x++) {
// Logger.log(x + JSON.stringify(TestArray["sheets"][0]["data"][0]["rowData"][x]));
// }
var rowData = TestArray["sheets"][0]["data"][0]["rowData"];
var backgroundColors = rowData.reduce((ar, r, i) => {
if ("values" in r) {
r.values.forEach((c, j) => {
if ("effectiveFormat" in c) {
ar.push({rowNumber: i + 1, columnNumber: j + 1, backgroundColor: c.effectiveFormat.backgroundColor});
}
});
}
return ar;
}, []);
console.log(backgroundColors)
结果:
使用上述脚本时,检索到以下结果。
[
{"rowNumber":1,"columnNumber":1,"backgroundColor":{"blue":1,"red":1,"green":1}},
,
,
,
]
- 在这种情况下,
rowNumber
和columnNumber
是单元格的坐标。
参考文献:
问题 2 的答案:
- 您想从 sheet 中检索
effectiveFormat
的背景颜色,并希望将检索到的值用作 batchUpdate 的 userEnteredFormat
的背景颜色。
您想在以下脚本中使用 colorrows
的检索值。
var spreadsheetId = "1eAq-RbtrCSMRPZ0p7XIpG3vd29yL-3SQ3D3JGyiUhKg";
var result = Sheets.Spreadsheets.batchUpdate({
requests: [{
updateCells: {
rows: colorrows,
fields: 'userEnteredFormat.backgroundColor',
start: {
sheetId: 1616717220,
rowIndex: 0,
columnIndex: 0
}
}
}]
}, spreadsheetId)
示例脚本:
在这种情况下,需要将属性名称从effectiveFormat
更改为userEnteredFormat
。
var TestArray = Sheets.Spreadsheets.get("1pcIKNUFmkk0d-UGg1sXl5xbsJC2WhocIHpM3et-CMgo", {
ranges:"TestBackgroundSheet!A1:AD39",
fields:"sheets(data(rowData(values(effectiveFormat.backgroundColor))))"
});
// for (var x = 0; x < 40; x++) {
// Logger.log(x + JSON.stringify(TestArray["sheets"][0]["data"][0]["rowData"][x]));
// }
var rowData = TestArray["sheets"][0]["data"][0]["rowData"];
var colorrows = rowData.reduce((ar, r, i) => {
var temp = [];
if ("values" in r) {
r.values.forEach((c, j) => {
temp.push("effectiveFormat" in c ? {userEnteredFormat: {backgroundColor: c.effectiveFormat.backgroundColor}} : {});
});
}
ar.push({values: temp});
return ar;
}, []);
- 在这种情况下,
colorrows
可以用于batchUpdate的上述脚本。
我一直在尝试将背景颜色批量更新回跨页sheet。社区在帮助我
当前代码:
var TestArray = Sheets.Spreadsheets.get("1pcIKNUFmkk0d-UGg1sXl5xbsJC2WhocIHpM3et-CMgo", {
ranges:"TestBackgroundSheet!A1:AD39",
fields:"sheets(data(rowData(values(effectiveFormat.backgroundColor))))"
});
for (var x = 0; x < 40; x++) {
Logger.log(x + JSON.stringify(TestArray["sheets"][0]["data"][0]["rowData"][x]));
}
var rowData = TestArray["sheets"][0]["data"][0]["rowData"]
.map(row => row.getValues()).toString()
var backgroundColors = JSON.parse("[" + rowData + "]")
.map(value => {
let v = value["effectiveFormat"]
return v ? v["backgroundColor"] : null
})
这returns一个错误如下SyntaxError: Unexpected token , in JSON at position 1 (line 184, file "macros")
。其中第 184 行是 var backgroundColors = JSON.parse("[" + rowData + "]")
。 Logger 语句结果的前几行在下面 posted:
0{}
[20-10-21 18:05:08:257 PDT] 1{"values":[{},{"effectiveFormat":{"backgroundColor":{"red":1,"blue":1,"green":1}}}]}
[20-10-21 18:05:08:260 PDT] 2{}
[20-10-21 18:05:08:262 PDT] 3{}
[20-10-21 18:05:08:265 PDT] 4{"values":[{"effectiveFormat":{"backgroundColor":{"red":1,"blue":1,"green":1}}},{"effectiveFormat":{"backgroundColor":{"blue":1,"green":1,"red":1}}},{},{"effectiveFormat":{"backgroundColor":{"green":1,"red":1,"blue":1}}},{"effectiveFormat":{"backgroundColor":{"blue":1,"red":1,"green":1}}},{},{"effectiveFormat":{"backgroundColor":{"green":1,"red":1,"blue":1}}},{"effectiveFormat":{"backgroundColor":{"red":1,"blue":1,"green":1}}},{},{"effectiveFormat":{"backgroundColor":{"green":1,"blue":1,"red":1}}},{"effectiveFormat":{"backgroundColor":{"blue":1,"green":1,"red":1}}},{},{"effectiveFormat":{"backgroundColor":{"green":1,"blue":1,"red":1}}},{"effectiveFormat":{"backgroundColor":{"blue":1,"red":1,"green":1}}},{},{"effectiveFormat":{"backgroundColor":{"green":1,"red":1,"blue":1}}},{"effectiveFormat":{"backgroundColor":{"green":1,"red":1,"blue":1}}},{},{"effectiveFormat":{"backgroundColor":{"green":1,"blue":1,"red":1}}},{"effectiveFormat":{"backgroundColor":{"red":1,"green":1,"blue":1}}},{},{"effectiveFormat":{"backgroundColor":{"red":1,"green":1,"blue":1}}},{"effectiveFormat":{"backgroundColor":{"blue":1,"red":1,"green":1}}},{},{"effectiveFormat":{"backgroundColor":{"green":1,"red":1,"blue":1}}},{"effectiveFormat":{"backgroundColor":{"green":1,"blue":1,"red":1}}},{},{"effectiveFormat":{"backgroundColor":{"red":1,"green":1,"blue":1}}},{"effectiveFormat":{"backgroundColor":{"red":1,"green":1,"blue":1}}}]}
正如我所见,代码的第一行是空的,我不确定为什么它暗示位置 1 中有一个意想不到的“,”。将不胜感激关于如何正确映射它的建议。
编辑#1:
我的总体目标是提取一个包含完整 sheets 背景颜色的数组,然后对该数组进行调整,然后将其批量更新回相同的 sheet。在社区的指导下,我得到了一个简化但实用的版本。此代码如下。
function myFunction() {
var TestArray = Sheets.Spreadsheets.get("1eAq-RbtrCSMRPZ0p7XIpG3vd29yL-3SQ3D3JGyiUhKg", {
ranges:"Awesome!A1:C3",
fields:"sheets(data(rowData(values(effectiveFormat.backgroundColor))))"
});
var backgroundColors = TestArray["sheets"][0]["data"][0]["rowData"]
.map(row => row["values"]
.map(value => value["effectiveFormat"]["backgroundColor"]));
var TotalText = Sheets.Spreadsheets.Values.get("1eAq-RbtrCSMRPZ0p7XIpG3vd29yL-3SQ3D3JGyiUhKg", "Awesome!A1:C3").values;
//Map Background Colors
var colorrows = backgroundColors.map(rowColors => {
return {
values: rowColors.map(cellColor => {
return {
userEnteredFormat: {
backgroundColor: cellColor
}
}
})
}
})
var spreadsheetId = "1eAq-RbtrCSMRPZ0p7XIpG3vd29yL-3SQ3D3JGyiUhKg";
var result = Sheets.Spreadsheets.batchUpdate({
requests: [{
updateCells: {
rows: colorrows,
fields: 'userEnteredFormat.backgroundColor',
start: {
sheetId: 1616717220,
rowIndex: 0,
columnIndex: 0
}
}
}]
}, spreadsheetId)
}
现在,当我尝试在更复杂的传播中使用此代码时sheet,我 运行 遇到了一些错误(通常是因为单元格为空),这使我遇到了错误 SyntaxError: Unexpected token , in JSON at position 1 (line 184, file "macros")
来自这个问题。
你现在已经修复了那个错误(我非常感谢)并给我留下了一个带有输出的数组。
[
{"rowNumber":1,"columnNumber":1,"backgroundColor":{"blue":1,"red":1,"green":1}},
,
,
,
]
原始的简化但功能测试 sheet 将输出一个数组,该数组将被输入到 batchupdate 代码中。此函数的格式如下。
[20-10-23 11:32:04:093 EDT] {values=[{userEnteredFormat={backgroundColor={red=1.0}}}, {userEnteredFormat={backgroundColor={green=1.0, red=1.0, blue=1.0}}}, {userEnteredFormat={backgroundColor={red=1.0, blue=1.0, green=1.0}}}]}
[20-10-23 11:32:04:098 EDT] {values=[{userEnteredFormat={backgroundColor={red=1.0, blue=1.0, green=1.0}}}, {userEnteredFormat={backgroundColor={green=1.0, red=1.0}}}, {userEnteredFormat={backgroundColor={green=1.0, blue=1.0, red=1.0}}}]}
[20-10-23 11:32:04:102 EDT] {values=[{userEnteredFormat={backgroundColor={blue=1.0, green=1.0, red=1.0}}}, {userEnteredFormat={backgroundColor={red=1.0, blue=1.0, green=1.0}}}, {userEnteredFormat={backgroundColor={red=0.2901961, green=0.5254902, blue=0.9098039}}}]}
我想知道您的代码 -> {"rowNumber":1,"columnNumber":1,"backgroundColor":{"blue":1,"red":1,"green":1}},
的输出是否需要转换为 -> {values=[{userEnteredFormat={backgroundColor={blue=1.0, green=1.0, red=1.0}}}
格式,然后再放回此代码中。
var spreadsheetId = "1eAq-RbtrCSMRPZ0p7XIpG3vd29yL-3SQ3D3JGyiUhKg";
var result = Sheets.Spreadsheets.batchUpdate({
requests: [{
updateCells: {
rows: colorrows,
fields: 'userEnteredFormat.backgroundColor',
start: {
sheetId: 1616717220,
rowIndex: 0,
columnIndex: 0
}
}
}]
}, spreadsheetId)
问题 1 的答案:
修改点:
- 在你的脚本中,我认为需要检查
rowData
的数组中是否有values
的属性,以及是否有[values
数组中effectiveFormat
的 =81=]。- 我认为这就是您出错的原因。
当这些点反映到你的脚本中,就变成了下面这样。
修改后的脚本:
var TestArray = Sheets.Spreadsheets.get("1pcIKNUFmkk0d-UGg1sXl5xbsJC2WhocIHpM3et-CMgo", {
ranges:"TestBackgroundSheet!A1:AD39",
fields:"sheets(data(rowData(values(effectiveFormat.backgroundColor))))"
});
// for (var x = 0; x < 40; x++) {
// Logger.log(x + JSON.stringify(TestArray["sheets"][0]["data"][0]["rowData"][x]));
// }
var rowData = TestArray["sheets"][0]["data"][0]["rowData"];
var backgroundColors = rowData.reduce((ar, r, i) => {
if ("values" in r) {
r.values.forEach((c, j) => {
if ("effectiveFormat" in c) {
ar.push({rowNumber: i + 1, columnNumber: j + 1, backgroundColor: c.effectiveFormat.backgroundColor});
}
});
}
return ar;
}, []);
console.log(backgroundColors)
结果:
使用上述脚本时,检索到以下结果。
[
{"rowNumber":1,"columnNumber":1,"backgroundColor":{"blue":1,"red":1,"green":1}},
,
,
,
]
- 在这种情况下,
rowNumber
和columnNumber
是单元格的坐标。
参考文献:
问题 2 的答案:
- 您想从 sheet 中检索
effectiveFormat
的背景颜色,并希望将检索到的值用作 batchUpdate 的userEnteredFormat
的背景颜色。您想在以下脚本中使用
colorrows
的检索值。var spreadsheetId = "1eAq-RbtrCSMRPZ0p7XIpG3vd29yL-3SQ3D3JGyiUhKg"; var result = Sheets.Spreadsheets.batchUpdate({ requests: [{ updateCells: { rows: colorrows, fields: 'userEnteredFormat.backgroundColor', start: { sheetId: 1616717220, rowIndex: 0, columnIndex: 0 } } }] }, spreadsheetId)
示例脚本:
在这种情况下,需要将属性名称从effectiveFormat
更改为userEnteredFormat
。
var TestArray = Sheets.Spreadsheets.get("1pcIKNUFmkk0d-UGg1sXl5xbsJC2WhocIHpM3et-CMgo", {
ranges:"TestBackgroundSheet!A1:AD39",
fields:"sheets(data(rowData(values(effectiveFormat.backgroundColor))))"
});
// for (var x = 0; x < 40; x++) {
// Logger.log(x + JSON.stringify(TestArray["sheets"][0]["data"][0]["rowData"][x]));
// }
var rowData = TestArray["sheets"][0]["data"][0]["rowData"];
var colorrows = rowData.reduce((ar, r, i) => {
var temp = [];
if ("values" in r) {
r.values.forEach((c, j) => {
temp.push("effectiveFormat" in c ? {userEnteredFormat: {backgroundColor: c.effectiveFormat.backgroundColor}} : {});
});
}
ar.push({values: temp});
return ar;
}, []);
- 在这种情况下,
colorrows
可以用于batchUpdate的上述脚本。