Google 网络应用程序使用 URLFetchApp 时出错:插入新行时范围超出网格限制
Error in Google webapp using URLFetchApp: range exceeds grid limits when inserting new row
在前面的步骤 中,我写信给 sheet 几乎可以在 Web 应用程序的服务帐户凭据下使用 URLFetchApp。代码(下方)仅适用于“put”方法; “post”产生一个错误。我的理解是更新和插入都可以用“put”方法完成。但是当范围用于新行时,代码会生成错误——也就是说,sheet 中的最后一行不是空白。
“插入新行时范围超出网格限制”
如果需要有空行(例如,添加 1000 行),这是否会变成手动维护工作来监视和定期添加新的空行?我希望“put”只是添加一个新行。 URLFetchApp 是否有另一种方法可以在插入数据之前先将行添加到“网格”?感谢您的指导!
function postData(ssID,updateRange,data) {
if (clientToken) {
var url= `https://sheets.googleapis.com/v4/spreadsheets/${ssID}/values/${updateRange}?valueInputOption=RAW`
var options = {
muteHttpExceptions: true,
contentType: 'application/json',
method:'put',
payload: JSON.stringify(data),
headers: { Authorization: 'Bearer ' + clientToken }
};
var response= UrlFetchApp.fetch(url,options);
var responseCode = response.getResponseCode();
var responseBody = response.getContentText();
if (responseCode === 200) {
var responseJson = JSON.parse(responseBody)
return responseJson;
} else {
Logger.log(Utilities.formatString("Request failed. Expected 200, got %d: %s", responseCode, responseBody))
return responseCode;
}
} else {//handle failed authorization }
} //end postData
试试这个:
function postData(ssID, updateRange, data) {
const ss = SpreadsheetApp.openById(ssID);
const sh = ss.getRange(updateRange).getSheet();//I assumed range is in A1 notation
const lr = sh.getLastRow();
if (lr == sh.getMaxRows()) {
sh.insertRowsAfter(lr, data.length);
}
if (clientToken) {
var url = `https://sheets.googleapis.com/v4/spreadsheets/${ssID}/values/${updateRange}?valueInputOption=RAW`
var options = {
muteHttpExceptions: true,
contentType: 'application/json',
method: 'put',
payload: JSON.stringify(data),
headers: { Authorization: 'Bearer ' + clientToken }
};
var response = UrlFetchApp.fetch(url, options);
var responseCode = response.getResponseCode();
var responseBody = response.getContentText();
if (responseCode === 200) {
var responseJson = JSON.parse(responseBody)
return responseJson;
} else {
Logger.log(Utilities.formatString("Request failed. Expected 200, got %d: %s", responseCode, responseBody))
return responseCode;
}
} else {//handle failed authorization }
} //end postData
很遗憾,Sheets API V4中没有方法可以在给定范围超过[=50=时自动添加行]范围。
您可以做的是通过使用 Method: spreadsheets.get then compare it with the range you will use to update values. If rowCount is less than the update range, use Method: spreadsheets.batchUpdate 更新 sheet 维度来获取特定 sheet 的 rowCount。
演示:
示例Sheet:
代码:
function request(ssID="spreadsheet id here",updateRange = "A1499:A1500",data) {
if (clientToken) {
var ssGetUrl= `https://sheets.googleapis.com/v4/spreadsheets/${ssID}`
var options = {
muteHttpExceptions: true,
contentType: 'application/json',
method:'get',
headers: { Authorization: 'Bearer ' + clientToken }
};
var ssGetresponse= JSON.parse(UrlFetchApp.fetch(ssGetUrl,options));
var sheets = ssGetresponse.sheets;
var rowCount = 0;
var sheetId = 0;
sheets.forEach(sheet => {
if(sheet.properties.sheetId == sheetId){
rowCount = sheet.properties.gridProperties.rowCount
}
})
var num = parseInt(updateRange.split(":")[1].replace(/[^0-9]/g,'')); //remove letters in updateRange and convert it to string
if(rowCount < num){
var diff = num - rowCount;
var resource = {
"requests": [
{
"appendDimension": {
"length": diff,
"dimension": "ROWS",
"sheetId": 0
}
}
]
};
var ssBatchUpdateUrl= `https://sheets.googleapis.com/v4/spreadsheets/${ssID}:batchUpdate`
var options = {
muteHttpExceptions: true,
contentType: 'application/json',
method:'post',
payload: JSON.stringify(resource),
headers: { Authorization: 'Bearer ' + clientToken }
};
var response= JSON.parse(UrlFetchApp.fetch(ssBatchUpdateUrl,options));
}
//insert code for updating range values
}
}
执行代码后:
注意:上面的demo是在更新范围超出sheet的实际行数时增加行数。例如,如果列的范围超出 sheet 的实际列,则可以更新脚本以同时读取 columnCount,将解析器修改为也获取 A1 表示法的列部分并在 appendDimension
上添加另一个条目,其中 COLUMN 作为维度。
在前面的步骤
“插入新行时范围超出网格限制”
如果需要有空行(例如,添加 1000 行),这是否会变成手动维护工作来监视和定期添加新的空行?我希望“put”只是添加一个新行。 URLFetchApp 是否有另一种方法可以在插入数据之前先将行添加到“网格”?感谢您的指导!
function postData(ssID,updateRange,data) {
if (clientToken) {
var url= `https://sheets.googleapis.com/v4/spreadsheets/${ssID}/values/${updateRange}?valueInputOption=RAW`
var options = {
muteHttpExceptions: true,
contentType: 'application/json',
method:'put',
payload: JSON.stringify(data),
headers: { Authorization: 'Bearer ' + clientToken }
};
var response= UrlFetchApp.fetch(url,options);
var responseCode = response.getResponseCode();
var responseBody = response.getContentText();
if (responseCode === 200) {
var responseJson = JSON.parse(responseBody)
return responseJson;
} else {
Logger.log(Utilities.formatString("Request failed. Expected 200, got %d: %s", responseCode, responseBody))
return responseCode;
}
} else {//handle failed authorization }
} //end postData
试试这个:
function postData(ssID, updateRange, data) {
const ss = SpreadsheetApp.openById(ssID);
const sh = ss.getRange(updateRange).getSheet();//I assumed range is in A1 notation
const lr = sh.getLastRow();
if (lr == sh.getMaxRows()) {
sh.insertRowsAfter(lr, data.length);
}
if (clientToken) {
var url = `https://sheets.googleapis.com/v4/spreadsheets/${ssID}/values/${updateRange}?valueInputOption=RAW`
var options = {
muteHttpExceptions: true,
contentType: 'application/json',
method: 'put',
payload: JSON.stringify(data),
headers: { Authorization: 'Bearer ' + clientToken }
};
var response = UrlFetchApp.fetch(url, options);
var responseCode = response.getResponseCode();
var responseBody = response.getContentText();
if (responseCode === 200) {
var responseJson = JSON.parse(responseBody)
return responseJson;
} else {
Logger.log(Utilities.formatString("Request failed. Expected 200, got %d: %s", responseCode, responseBody))
return responseCode;
}
} else {//handle failed authorization }
} //end postData
很遗憾,Sheets API V4中没有方法可以在给定范围超过[=50=时自动添加行]范围。
您可以做的是通过使用 Method: spreadsheets.get then compare it with the range you will use to update values. If rowCount is less than the update range, use Method: spreadsheets.batchUpdate 更新 sheet 维度来获取特定 sheet 的 rowCount。
演示:
示例Sheet:
代码:
function request(ssID="spreadsheet id here",updateRange = "A1499:A1500",data) {
if (clientToken) {
var ssGetUrl= `https://sheets.googleapis.com/v4/spreadsheets/${ssID}`
var options = {
muteHttpExceptions: true,
contentType: 'application/json',
method:'get',
headers: { Authorization: 'Bearer ' + clientToken }
};
var ssGetresponse= JSON.parse(UrlFetchApp.fetch(ssGetUrl,options));
var sheets = ssGetresponse.sheets;
var rowCount = 0;
var sheetId = 0;
sheets.forEach(sheet => {
if(sheet.properties.sheetId == sheetId){
rowCount = sheet.properties.gridProperties.rowCount
}
})
var num = parseInt(updateRange.split(":")[1].replace(/[^0-9]/g,'')); //remove letters in updateRange and convert it to string
if(rowCount < num){
var diff = num - rowCount;
var resource = {
"requests": [
{
"appendDimension": {
"length": diff,
"dimension": "ROWS",
"sheetId": 0
}
}
]
};
var ssBatchUpdateUrl= `https://sheets.googleapis.com/v4/spreadsheets/${ssID}:batchUpdate`
var options = {
muteHttpExceptions: true,
contentType: 'application/json',
method:'post',
payload: JSON.stringify(resource),
headers: { Authorization: 'Bearer ' + clientToken }
};
var response= JSON.parse(UrlFetchApp.fetch(ssBatchUpdateUrl,options));
}
//insert code for updating range values
}
}
执行代码后:
注意:上面的demo是在更新范围超出sheet的实际行数时增加行数。例如,如果列的范围超出 sheet 的实际列,则可以更新脚本以同时读取 columnCount,将解析器修改为也获取 A1 表示法的列部分并在 appendDimension
上添加另一个条目,其中 COLUMN 作为维度。