Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
821 views
in Technique[技术] by (71.8m points)

google apps script - How to get a data range of the Sheet

I need to get a data range of the Sheet. I must call APIs twice.

GET https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}/values/Sheet1?majorDimension=ROWS
// Height is result.length

GET https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}/values/Sheet1?majorDimension=COLUMNS
// Width is result.length

But I do not need data, I only need the size of the data range. Is there a way to get that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Unfortunately, I couldn't find the method for directly retrieving "the size of the data range" using Sheets APIs. So as a workaround, I would like to propose to use Web Apps. In your case, it seems that you already have an access token. So this proposal accesses to Web Apps using the access token. The flow of this proposal is as follows.

1. Copy and paste this sample script

function doGet(e) {
  var id = e.parameter.id;
  var sheetName = e.parameter.sheetname;
  var result = {};
  try {
    var ss = SpreadsheetApp.openById(id).getSheetByName(sheetName);
    result.LastRow = ss.getLastRow();
    result.LastColumn = ss.getLastColumn();
  } catch(er) {
    result.Error = er;
  }
  return ContentService.createTextOutput(JSON.stringify(result)).setMimeType(ContentService.MimeType.JSON);
}

After copied and pasted this, please directly run doGet(). By this, the authorization can be done. Please ignore the error of script.

2. Deploy Web Apps :

The condition for deploying Web Apps is as follows.

  • On script editor on the project with doGet().
    • Publish -> Deploy as web app
    • For "Execute the app as:", set "Me".
    • For "Who has access to the app:", set "Only myself".
    • Click Deploy.
    • Copy URL of Web Apps.

At above condition, you can access to Web Apps only when the access token is used. When the access token is not used, the error of Unauthorized returns. In this case, please include https://www.googleapis.com/auth/drive to the scopes.

3. Curl sample :

This is a sample curl. Using this, you can retrieve LastRow and LastColumn of the spreadsheet. When you use this, please replace URL to your Web Apps's URL, and input spreadsheet ID and sheet name.

curl -GL 
  -H "Authorization: Bearer ### your access token ###" 
  -d "id=### spreadsheet ID ###" 
  -d "sheetname=### sheet name ###" 
  "https://script.google.com/macros/s/#####/exec"
Sample result :
{"LastRow":10,"LastColumn":10}

If this was not useful for you, I'm sorry.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...