Google App脚本-如果选项卡的名称在另一个数组中不存在,则删除该选项卡

时间:2019-04-22 16:58:00

标签: google-apps-script

我正在尝试提取所有现有选项卡的名称,并将其与在这种情况下为kANew的另一个数组进行比较。

下面是我使用的代码...

 function Checking() {

  var reportId = xxx
  var profileId = yyy;

  var httpOptions = {'headers': {'Authorization': 'Bearer ' + 
ScriptApp.getOAuthToken()}};       
  var additionalParameters = {'synchronous': 'true'};

  //run report and get file ID
  var ReportFile = eXelate.Reports.run(profileId, reportId, 
additionalParameters);
  var ReportFileID = (ReportFile.id);

  //
  var newReportFile = eXelate.Files.get(reportId, 
ReportFileID);
  if(newReportFile.urls) {var httpOptions = {'headers': {'Authorization': 
'Bearer ' + ScriptApp.getOAuthToken()}};
  var csvContent = UrlFetchApp.fetch(newReportFile.urls.apiUrl, 
httpOptions).getContentText();
  var csvData = Utilities.parseCsv(csvContent);

  //past data into sheet                        
  var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
  sheet.insertRowsAfter(sheet.getMaxRows(), 6000);
  sheet.deleteRows(1,6000);
  sheet.getRange(1, 1, csvData.length, 
csvData[0].length).setValues(csvData);

  //delete beginning and report totals
  sheet.deleteRows(1, 11);
  var sheets = new Array()
  var activess = SpreadsheetApp.getActive();
  var sheetss = SpreadsheetApp.getActiveSpreadsheet().getSheets();
  for (var i = 0; i < sheetss.length ; i++) sheets.push( [sheetss[i].getName() ] )
  var lastrow = sheet.getLastRow();
      sheet.deleteRows(lastrow);
  var rows = sheet.getDataRange();                        
  var numRows = rows.getNumRows();
  var values = rows.getValues();
  var rowsDeleted = 0;
  var rawvalues = sheet.getRange(2,1,lastrow,1).getValues();
  var rawvaluestring = rawvalues.toString();
  var sheetsforremoval = sheets.toString();
  var lR = sheet.getDataRange().getNumRows();
  var kA = sheet.getRange(2,1,lR,1).getValues();
  var kANew = [].concat.apply([],kA);
   for (var i = 0; i < kANew.length; i ++){
  kANew[i] += ' Placements'
  // Make sure to push the actual raw data name and keep it in the array 
  //so that the deleteSheet doesn`t recognize raw data name as something to delete!!!!!!!!!
   }
  kANew.push('Sheet1')

      for (var i = 0; i <= numRows - 1; i++) {
    var row = values[i];
    if (row[2] == 0 || row[2] == '') {
      sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
      rowsDeleted++; 
    }
    }

                         }

  var obj = {};
  var ret_arr = [];
  for (var i = 0; i < kANew.length; i++) {
    obj[kANew[i]] = true;
  }
  for (var key in obj) {
    ret_arr.push(key);
  }
  Logger.log(ret_arr)
  for (var i = 0; i < ret_arr.length; i ++) {
    if (ret_arr.indexOf(sheetss[i]) == -1) { // what is wrong with this getName function?
    activess.deleteSheet(sheetss[i])
    }
  }
}

当我运行此代码时,它将删除当前Google工作表中存在的所有标签。如果选项卡的实际名称不在此ret_arr数组中,则应该删除这些选项卡。

所以基本上,这背后的逻辑是:a)将sheets [i]与ret_arr比较 b)如果在ret_arr数组中找不到sheets [i]中的元素,则相应地删除该选项卡。

  • 旁注:当我使用“ .getName()”函数尝试此操作时,它返回一条错误消息:“无法获取未定义的.getName()。

下面的示例...

  for (var i = 0; i < ret_arr.length; i ++) {
    if (ret_arr.indexOf(sheetss[i].getName()) == -1) { // what is wrong with this getName function?
    activess.deleteSheet(sheetss[i])
    }
  }
}

有人可以给我建议我应该在哪里修复,以便它只能删除相关的标签吗?

1 个答案:

答案 0 :(得分:0)

尝试一下:

 colors = [
     {id: '1', label: 'Red'},
     {id: '2', label: 'Yellow'},
     {id: '3', label: 'Green'}
 ]