如何在Google Sheets脚本中调用UpdateCellsRequest



我正试图修复我的谷歌表脚本程序,这是超时。我正在合并一组由字母网格组成的表格,这些字母网格用颜色突出显示,以识别网格中的形状。我想展示一个热图,也有个别形状的特定分组表。我的工作,但超时一旦我开始添加许多单独的形状表,所以我已经修复了它,让一切准备好,然后调用一些描述的batchUpdate。这可能吗?我可以批处理一系列设置背景颜色和设置粗体和斜体的一系列单独的单元格在一个调用内的脚本?下面是我的代码,我如何调用UpdateCellsRequest()

function createBatchMerge(target_name){
//  var range=lookup[target_name].getRange('A2:S9')
for (const target_name in merge_store){
var sheetId=lookup[target_name].getSheetId()
var instructions=merge_store[target_name]
for (const cellc in instructions){
var instruction=instructions[cellc]
//      Logger.log("CELLC="+cellc)
var [row,col]=cellc.split(':')
//      Logger.log("ROW="+row+" col="+col)
var i={
updateCells:{
rows:[{
values:[{
"userEnteredFormat": {
backgroundColor:instruction.colour,
textFormat:{
italic:instruction.italic,
bold:instruction.bold
},
note:instruction.names.join(',')
}
}
]
}]
},
start:{
"sheetId": sheetId,
"rowIndex": 1+row,
"columnIndex": col
}
}
jobs.push(i)
}
}
}
function dumpMerge(){
//  Logger.log(jobs)
SpreadsheetApp.UpdateCellsRequest(jobs)
}
这是我的床单。我需要它在相关的脚本工作,所以我可以允许合作者更新的形状和添加新的,然后只是运行它。https://docs.google.com/spreadsheets/d/11bROsFF8hM9SHJtH4WewxnuZ1Z9tuB8JUFR5MM7Ga7o/edit?usp=sharing

问题:

  • SpreadsheetApp没有任何UpdateCellsRequest方法。如果你想调用UpdateCellsRequest,启用Advanced Sheets Service(参见如何启用)。
  • 你没有提供一个有效的请求体:(1)它应该包括一个requests字段,(2)它应该包括一个fields字段(见这里),(3)note不是一个有效的字段;我不确定你想在这里更新什么信息,但它应该添加到不同的字段。

解决方案:

考虑到所有这些,下面提供了一个可能的请求体。请注意,这可能不能满足您的所有目的,因为您想要更新的确切信息并不清楚。在任何情况下,它都可以作为一个指南。

代码示例:

const payload = {
requests: [
{
updateCells: {
rows:[{
values:[{
userEnteredFormat: {
textFormat: {
italic: instruction.italic,
bold: instruction.bold
}
}        
}]
}],
start: { 
sheetId: sheetId,
rowIndex: 1+row,
columnIndex: col
},
fields: "userEnteredFormat.textFormat" // Fields you want to update
}
}
]
}
const spreadsheetId = SpreadsheetApp.getActive().getId();
Sheets.Spreadsheets.batchUpdate(payload, spreadsheetId);

最新更新