Мне нужно выбрать диапазон моего листа из 17-й строки («A17: J») для сводной таблицы - PullRequest
0 голосов
/ 07 мая 2020

Код работает нормально, как требуется, но мне нужна моя сводка на том же листе, и для этого я хотел бы изменить диапазон моих данных на («A17: J»), я пробовал разные способы, но не работал.

function createPivotTable() {
  var ss = SpreadsheetApp.getActive();
//  var myRange = ss.getRange("A17:J20");



  // The name of the sheet containing the data you want to put in a table.
  var sheetName = "PT";



  var pivotTableParams = {};

  // The source indicates the range of data you want to put in the table.
  // optional arguments: startRowIndex, startColumnIndex, endRowIndex, endColumnIndex
  pivotTableParams.source = {
    sheetId: ss.getSheetByName(sheetName).getSheetId()

  };

  // Group rows, the 'sourceColumnOffset' corresponds to the column number in the source range
  // eg: 0 to group by the first column
  pivotTableParams.rows = [{
    sourceColumnOffset: 3,
    showTotals: true,
    sortOrder: "ASCENDING"   

  }];

  // Defines how a value in a pivot table should be calculated.
  pivotTableParams.values = [{
    summarizeFunction: "SUM",
    sourceColumnOffset: 9,
    name:"Hours"},{
    summarizeFunction: "SUM",
      sourceColumnOffset: 6},{
    summarizeFunction: "SUM",
        sourceColumnOffset: 7,
        name:"Warm Leads"},{
      summarizeFunction: "CUSTOM",
      name: "Leads / Hour",
      formula: "=(sum('Hot Leads')+sum('Warm Leads'))/sum(Hours)"
  }];

  // Create a new sheet which will contain our Pivot Table
  var pivotTableSheet = ss.insertSheet();
  var pivotTableSheetId = pivotTableSheet.getSheetId();

  // Add Pivot Table to new sheet
  // Meaning we send an 'updateCells' request to the Sheets API
  // Specifying via 'start' the sheet where we want to place our Pivot Table
  // And in 'rows' the parameters of our Pivot Table
  var request = {
    "updateCells": {
      "rows": {
        "values": [{
          "pivotTable": pivotTableParams
        }]
      },
      "start": {
        "sheetId": pivotTableSheetId
      },
      "fields": "pivotTable"
    }
  };

  Sheets.Spreadsheets.batchUpdate({'requests': [request]}, ss.getId());
}

вы можете помочь мне, предоставив различные примеры кода, если они у вас есть, но если вы сможете исправить это, это будет более полезно.

Спасибо Musaddiq

1 Ответ

1 голос
/ 17 мая 2020

Следуя коду, который я использовал, чтобы получить то, что необходимо, проблема теперь решена путем настройки, остальной код не требует пояснений.

Спасибо всем за помощь.

  var ss = SpreadsheetApp.getActiveSpreadsheet();



// The name of the sheet containing the data you want to put in a table.
  var sheetName = ss.getSheetByName("Stats from SS Monitor");
  var lastRow = sheetName.getLastRow();
  var lastCol = sheetName.getLastColumn();



  var pivotTableParams = {};

  // The source indicates the range of data you want to put in the table.
  // optional arguments: startRowIndex, startColumnIndex, endRowIndex, endColumnIndex
  pivotTableParams.source = {
    sheetId: ss.getSheetByName("Stats from SS Monitor").getSheetId(),
    startRowIndex: 16,
    startColumnIndex: 0,
    endRowIndex: lastRow,
    endColumnIndex: lastCol

  };

  // Group rows, the 'sourceColumnOffset' corresponds to the column number in the source range
  // eg: 0 to group by the first column
  pivotTableParams.rows = [{
    sourceColumnOffset: 3,
    showTotals: true,
    sortOrder: "ASCENDING"   

  }];

  // Defines how a value in a pivot table should be calculated.
  pivotTableParams.values = [{
    summarizeFunction: "SUM",
    sourceColumnOffset: 9,
    name:"Hours"},{
    summarizeFunction: "SUM",
      sourceColumnOffset: 6},{
    summarizeFunction: "SUM",
        sourceColumnOffset: 7,
        name:"Warm Leads"},{
      summarizeFunction: "CUSTOM",
      name: "Leads / Hour",
      formula: "=(sum('Hot Leads')+sum('Warm Leads'))/sum('Hours')"
  }];

  // use same sheet which will contain our Pivot Table on top
    var pivotTableSheet = ss;
    var pivotTableSheetId = pivotTableSheet.getSheetId();


  // Meaning we send an 'updateCells' request to the Sheets API
  // Specifying via 'start' the sheet where we want to place our Pivot Table
  // And in 'rows' the parameters of our Pivot Table
  var request = {
    "updateCells": {
      "rows": {
        "values": [{
          "pivotTable": pivotTableParams
        }]
      },
      "start": {
        "sheetId": pivotTableSheetId
      },
      "fields": "pivotTable"
    }
  };

  Sheets.Spreadsheets.batchUpdate({'requests': [request]}, ss.getId());
}
...