Payload JSON non valido con Sheets.Spreadsheets.BatchUpdate
Tentativo di lavorare con Sheets.Spreadsheets.Get e Sheets. Sto cercando di ottenere la formattazione pull da un foglio di calcolo e incollare quella formattazione in un altro. Questa è semplicemente una prova di concetto per ulteriori applicazioni. Ricevo un errore di payload JSON con il codice seguente e non riesco a capire come formattarlo per inserire Array.
function Test() {
//sheets[].data[].rowData[].values[].cellData.effectiveFormat.backgroundColor
var TestArray = Sheets.Spreadsheets.get("1eAq-RbtrCSMRPZ0p7XIpG3vd29yL-3SQ3D3JGyiUhKg", {
ranges:"Awesome!A1:C3",
fields:"sheets(data(rowData(values(effectiveFormat.backgroundColor))))"
});
var spreadsheetId = "1eAq-RbtrCSMRPZ0p7XIpG3vd29yL-3SQ3D3JGyiUhKg";
var result = Sheets.Spreadsheets.batchUpdate({
requests: [{
updateCells: {
rows: [{
values: [{
userEnteredValue: {
stringValue: 'Test String'
}, userEnteredFormat: {
backgroundColor: TestArray
}
}]
}],//rows
fields: 'userEnteredValue.stringValue,userEnteredFormat.backgroundColor',
start: {
sheetId: 1616717220,
rowIndex: 0,
columnIndex: 0
}
}//update cell
}]//requests
}, spreadsheetId)
} ```
**EDIT:**
Rebuilt function copying both Text and Background colors.
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 Text
var textrows = TotalText.map(rowText => {
return {
values: rowText.map(cellText => {
return {
userEnteredValue: {
stringValue: cellText
}
}
})
}
})
//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: textrows,
fields: 'userEnteredValue.stringValue',
start: {
sheetId: 1616717220,
rowIndex: 0,
columnIndex: 0
}
}//update cell
},{
updateCells: {
rows: colorrows,
fields: 'userEnteredFormat.backgroundColor',
start: {
sheetId: 1616717220,
rowIndex: 0,
columnIndex: 0
}
}
}]
}, spreadsheetId)
}
Edit #2:
function myFunctionOneRequest() {
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 Text
var textrows = TotalText.map((rowText,i) => {
return {
values: rowText.map((cellText,j) => {
return {
userEnteredValue: {
stringValue: cellText
}
}
})
}
})
//Map Background Colors
var colorrows = backgroundColors.map((rowColors,k) => {
return {
values: rowColors.map((cellColor,l) => {
return {
userEnteredFormat: {
backgroundColor: cellColor
}
}
})
}
})
var spreadsheetId = "1eAq-RbtrCSMRPZ0p7XIpG3vd29yL-3SQ3D3JGyiUhKg";
var result = Sheets.Spreadsheets.batchUpdate({
requests: [{
updateCells: {
rows: textrows,
fields: 'userEnteredValue.stringValue',
start: {
sheetId: 1616717220,
rowIndex: 0,
columnIndex: 0
}
}//update cell
}]
}, spreadsheetId)
}
Risposte
Problema:
Stai fornendo a Spreadsheet resource ( TestArray, restituito da spreadsheets.get ) dove dovresti fornire un colore . Quindi, stai ricevendo un errore di payload JSON non valido.
Questo perché il fieldsparametro filtra i campi nidificati che verranno popolati nella risposta della tua prima chiamata, ma questi campi nidificati saranno comunque nidificati sul tuo JSON e dovrai accedervi specificando le proprietà padre corrispondenti.
Soluzione:
La risposta alla tua prima chiamata è qualcosa del tipo:
{
"sheets": [
{
"data": [
{
"rowData": [
{
"values": [
{
"effectiveFormat": {
"backgroundColor": {
"red": 1,
"green": 1,
"blue": 1
}
}
},
// Other cells in row
]
},
// Other rows in the requested range
]
} // Only one range is specified, so there's only one GridData element
]
},
// Other sheets
]
}
Quindi, ad esempio, se vuoi accedere alla backgroundColorprima cella della prima riga nell'intervallo richiesto, dovresti fare quanto segue:
var backgroundColor = TestArray["sheets"][0]["data"][0]["rowData"][0]
["values"][0]["effectiveFormat"]["backgroundColor"];
Oppure, in alternativa, se vuoi recuperare un array 2D backgroundColorsdi tutte le celle nell'intervallo richiesto, puoi farlo:
var backgroundColors = TestArray["sheets"][0]["data"][0]["rowData"]
.map(row => row["values"]
.map(value => value["effectiveFormat"]["backgroundColor"]));
Se desideri aggiornare più celle, devi modificare il corpo della richiesta di conseguenza, aggiungendo gli array aggiuntivi rowse valuescorrispondenti.
Modificare:
Ad esempio, se si desidera che le celle di destinazione abbiano gli stessi colori di sfondo dell'origine e che tutte abbiano il valore Test String, è possibile creare il corpo della richiesta in questo modo:
var rows = backgroundColors.map(rowColors => {
return {
values: rowColors.map(cellColor => {
return {
userEnteredValue: {
stringValue: 'Test String'
},
userEnteredFormat: {
backgroundColor: cellColor
}
}
})
}
})
var result = Sheets.Spreadsheets.batchUpdate({
requests: [{
updateCells: {
rows: rows,
fields: 'userEnteredValue.stringValue,userEnteredFormat.backgroundColor',
start: {
sheetId: 1616717220,
rowIndex: 0,
columnIndex: 0
}
}//update cell
}]//requests
}, spreadsheetId)
Se ogni cella deve avere valori stringa diversi, è necessario memorizzarli in un array 2D e fornirli all'interno dei mapmetodi, invece di Test Stringspecificare gli indici corrispondenti (forniti come parametro opzionale in ciascuno map).
Modifica 2:
Per aggiornare sia i valori che i colori di sfondo con la stessa richiesta, è sufficiente scorrere uno di essi con mape utilizzare i parametri di indice corrispondenti (sono parametri opzionali del mapmetodo, chiamato ie jnell'esempio sotto) per accedere ai diversi valori dell'altro.
Ad esempio, se backgroundColorse stringsgli array 2D che desideri utilizzare per creare rows, puoi farlo:
var backgroundColors = [["2D array with colors"]];
var strings = [["2D array with strings"]];
var rows = backgroundColors.map((rowColors,i) => {
return {
values: rowColors.map((cellColor,j) => {
return {
userEnteredValue: {
stringValue: strings[i][j]
},
userEnteredFormat: {
backgroundColor: cellColor
}
}
});
}
});