解决Sheets API获取大型数据集时的413错误及数据清理问题
使用Sheets API处理40万行大型数据集时的分批清理问题
我正在开展一个项目,尝试使用Sheets API获取包含约40万行、11列的大型数据集。我了解到可通过分批次(如每次10万行)加载数据,但不知如何在分批处理时完成数据清理——需要遍历行并移除Content列值属于36个指定值之一的行。以下是我尝试的脚本,但在获取大型数据集的dataSourceArr时遇到413错误,同时附上待排除数据、大型数据集及预期结果示例:
//Data from Content Column. These are the excluded items excludedContentArr = Sheets.Spreadsheets.Values.get(tss.getId(), `'` + excludedContentSht.getName() + `'!A2:A`).values; //This is the large dataset that is giving me an error right now dataSourceArr = Sheets.Spreadsheets.Values.get(sourceSS.getId(), `'` + dataSourceSht.getName() + `'!A1:` + columnToLetter(dataSourceSht.getLastColumn())).values; var headersDataSrcArr = dataSourceArr[0];//Get the first and only inner array var eidDataSrcCN = headersDataSrcArr.indexOf('EID') + 1;//Arrays are zero indexed- add 1. var contentDataSrcCN = headersDataSrcArr.indexOf('Content') + 1;//Arrays are zero indexed- add 1. var statusDataSrcCN = headersDataSrcArr.indexOf('Status') + 1;//Arrays are zero indexed- add 1. var compDateDataSrcCN = headersDataSrcArr.indexOf('Completion Date') + 1;//Arrays are zero indexed- add 1. var pathwayDataSrcCN = headersDataSrcArr.indexOf('Pathway') + 1;//Arrays are zero indexed- add 1. var values = []; for(var i in dataSourceArr){ var excludedContentIndex = excludedContentArr.map(r => r[0]).indexOf(dataSourceArr[i][contentDataSrcCN-1]); if(excludedContentIndex == -1){ //Action if not found values.push([ dataSourceArr[i][eidDataSrcCN-1], dataSourceArr[i][contentDataSrcCN-1], dataSourceArr[i][statusDataSrcCN-1], dataSourceArr[i][compDateDataSrcCN-1], dataSourceArr[i][pathwayDataSrcCN-1] ]); } } Logger.log('Found ' + values.length + ' rows of data that met the criteria.'); //Paste into the destination sheet. Sheets.Spreadsheets.Values.update({ values }, tss.getId(), `'` + pathwayContentCompSht.getName() + `'!A2`, { valueInputOption: "USER_ENTERED" });
待排除数据示例
| Content |
|---|
| Back to the Future 1 |
| Back to the Future 2 |
| Back to the Future 3 |
大型数据集示例
| EID | Content | Status | Completion Date | Pathway |
|---|---|---|---|---|
| a1 | Rocky 5 | Complete | 01/01/2024 | abc123 |
| a2 | Back to the Future 1 | Complete | 01/01/2024 | abc123 |
| a3 | Rocky 4 | Complete | 01/01/2024 | abc123 |
| a4 | Rocky 3 | Complete | 01/01/2024 | abc123 |
| a5 | Back to the Future 2 | Complete | 01/01/2024 | abc123 |
| a6 | Rocky 2 | Complete | 01/01/2024 | abc123 |
| a7 | Rocky 1 | Complete | 01/01/2024 | abc123 |
| a8 | Matrix Revolutions | Complete | 01/01/2024 | abc123 |
| a9 | Back to the Future 3 | Complete | 01/01/2024 | abc123 |
| a10 | Matrix | Complete | 01/01/2024 | abc123 |
预期结果示例
| EID | Content | Status | Completion Date | Pathway |
|---|---|---|---|---|
| a1 | Rocky 5 | Complete | 01/01/2024 | abc123 |
| a3 | Rocky 4 | Complete | 01/01/2024 | abc123 |
| a4 | Rocky 3 | Complete | 01/01/2024 | abc123 |
| a6 | Rocky 2 | Complete | 01/01/2024 | abc123 |
| a7 | Rocky 1 | Complete | 01/01/2024 | abc123 |
| a8 | Matrix Revolutions | Complete | 01/01/2024 | abc123 |
| a10 | Matrix | Complete | 01/01/2024 | abc123 |
内容的提问来源于stack exchange,提问作者DanCue
相关产品推荐
相关产品推荐

