【问题标题】:Find sheet size using Google Sheets API使用 Google Sheets API 查找图纸尺寸
【发布时间】:2021-04-30 14:55:38
【问题描述】:

我正在使用 API 连接到谷歌表格,并想找出表格的最大(现有)尺寸)以便我可以提取所有数据

$service = new Google_Service_Sheets($client);
// would really like to calculate range
$range = 'Sheet1!A1:Z100';
$response = $service->spreadsheets_values->get($spreadsheetId, $range);

有没有办法确定工作表的最大现有尺寸是多少,甚至获取整个工作表,以便我可以更精确地预先计算我需要的单元格范围?

【问题讨论】:

  • 您是否尝试过单独使用工作表标题作为范围参数?它不在文档中,但我读过它只会返回工作表的填充行。
  • 刚刚对它进行了非常快速测试,它似乎有效 - 干杯!我会在更好地检查后明天更新。

标签: php google-sheets-api


【解决方案1】:

我相信你的目标和你目前的情况如下。

  • 您希望从 Google 电子表格中的特定工作表中检索所有值。
  • 您想从 Google 电子表格中的特定工作表中检索单元格总数。
  • 您希望使用 googleapis for PHP 来实现此目的。
  • 您已经能够使用 Sheets API 从 Google 电子表格中获取值。

修改点:

  • 为了从 Google 电子表格中的特定工作表中检索所有值,不需要使用范围的 a1 表示法。在这种情况下,您可以使用工作表名称作为范围来检索所有值。
  • 为了从谷歌电子表格中的特定工作表中检索单元格总数,我认为可以使用使用电子表格检索的每个工作表的gridProperties。get 方法。 gridProperties 的对象具有rowCountcolumnCount 的属性。我认为这些属性可以用来计算单元格的总数。

当以上几点反映到你的脚本中时,它变成如下。

修改脚本一:

在此修改中,所有值都从特定工作表中检索。你的脚本被修改了,变成如下。

$service = new Google_Service_Sheets($client);
// would really like to calculate range
$range = 'Sheet1'; // Modified
$response = $service->spreadsheets_values->get($spreadsheetId, $range);

修改脚本2:

在此修改中,从特定工作表中检索单元格的总数。你的脚本被修改了,变成如下。在这种情况下,使用了电子表格.get的方法。

$service = new Google_Service_Sheets($client);
// would really like to calculate range
$range = 'Sheet1';
$response = $service->spreadsheets->get($spreadsheetId, ["ranges" => [$range], "fields" => "sheets(properties(gridProperties(columnCount,rowCount)))"]);
$gridProperties = $response[0] -> getProperties() -> getGridProperties();
$rowCount = $gridProperties -> getRowCount();
$columnCount = $gridProperties -> getColumnCount();
$totalNumberOfCells = $rowCount * $columnCount;
print($totalNumberOfCells);
  • 作为fields的值,我使用了sheets(properties(gridProperties(columnCount,rowCount)))

  • 如果您想从 Google 电子表格中的所有工作表中检索单元格总数,您还可以使用以下脚本。

      $spreadsheetId = "###";  // Please set the Spreadsheet ID.
      $response = $service->spreadsheets->get($spreadsheetId, ["fields" => "sheets(properties(gridProperties(columnCount,rowCount)))"]);
      $totalNumberOfCells = 0;
      foreach ($response as $i => $sheet) {
          $gridProperties = $sheet -> getProperties() -> getGridProperties();
          $rowCount = $gridProperties -> getRowCount();
          $columnCount = $gridProperties -> getColumnCount();
          $totalNumberOfCells += $rowCount * $columnCount;
      }
      print($totalNumberOfCells);
    

参考资料:

【讨论】:

  • 谢谢,能用@miken32的评论解决这个问题,但是这个详细的解释也很有用。
【解决方案2】:

如果您担心接近 5 百万个单元格的限制,那么此纸张大小调整器应该适合您。它使用一些脚本和 HTML。

const MAX_SHEET_CELLS = 5000000;

 function onOpen(e) {
   SpreadsheetApp.getUi()
       .createMenu('Extras')
       .addItem('Open Sheet Sizer', 'showSidebar')
   .addToUi(); 
}

/**
* function to show sidebar
*/
function showSidebar() {
 
  // create sidebar with HTML Service
  const html = HtmlService.createHtmlOutputFromFile('Sidebar').setTitle('Sheet Sizer');
 
// add sidebar to spreadsheet UI
  SpreadsheetApp.getUi().showSidebar(html);
}

/**
* Get size data for a given sheet url
*/
function auditSheet(sheet) {
 
  // get spreadsheet object
  const ss = SpreadsheetApp.getActiveSpreadsheet();
 
  // get sheet name
  const name = sheet.getName();
 
  // get current sheet dimensions
  const maxRows = sheet.getMaxRows();
  const maxCols = sheet.getMaxColumns();
  const totalCells = maxRows * maxCols;
 
  // put variables into object
  const sheetSize = {
    name: name,
    rows: maxRows,
    cols: maxCols,
    total: totalCells
  }
 
  // return object to function that called it
  return sheetSize;
 
}

/**
* Audits all Sheets and passes full data back to sidebar
*/
function auditAllSheets() {
 
  // get spreadsheet object
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheets = ss.getSheets();
 
  // declare variables
  let output = '';
  let grandTotal = 0;
 
  // loop over sheets and get data for each
  sheets.forEach(sheet => {
 
    // get sheet results for the sheet
    const results = auditSheet(sheet);
     
    // create output string from results
    output = output + '<br><hr><br>Sheet: ' + results.name +
      '<br>Row count: ' + results.rows + 
      '<br>Column count: ' + results.cols +
      '<br>Total cells: ' + results.total + '<br>';
 
    // add results to grand total
    grandTotal = grandTotal + results.total;
 
  });
 
  // add grand total calculation to the output string
  output = output + '<br><hr><br>' + 
    'You have used ' + ((grandTotal / 5000000)*100).toFixed(2) + '% of your 5 million cell limit.';
 
  // pass results back to sidebar
  return output;
 
}

HTML:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  <!-- Add CSS code to format the sidebar from google stylesheet -->
  <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
  
  <style>
    body {
      padding: 20px;
    }
  </style>
  </head>
  <body>
  <div class="block">
    <input type="button" value="Get Sheet Size" onclick="getSheetSize()" />
  </div>
  <div class="block">
    <input type="button" value="Close" onclick="google.script.host.close()" />
  </div>
  <div class="block">
    <div id="results"></div>  
  </div>
 <script>
 function getSheetSize() {
  //google.script.run.auditSheet();
  //google.script.run.withSuccessHandler(displayResults).auditSheet();
  google.script.run.withSuccessHandler(displayResults).auditAllSheets();

 }

 function displayResults(results) {
  // display results in sidebar
  document.getElementById("results").innerHTML = results;
}

 </script>
  </body>
</html>

我在一个示例游戏表中有这个,如果您有兴趣,可以复制一份

https://docs.google.com/spreadsheets/d/1qbLOjTdzISICTKyUp_jK6gZbQCt-OwtDYYy3HNJygeE/edit#gid=1181136581

【讨论】:

  • 想要一个 PHP 解决方案而不是 JavaScript,但这在未来可能会有用 - 谢谢
猜你喜欢
  • 2011-10-06
  • 1970-01-01
  • 1970-01-01
  • 2017-09-25
  • 2010-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多