How to Perform a Lookup/Filter Across Google Spreadsheets Using Google Apps Script

谁说我不能喝 提交于 2019-12-08 12:24:52

问题


I would like to use Google Apps Script to populate a spreadsheet with filtered data from another spreadsheet.

The source document is a list of employees (column A, NAME), with their reported hours (column B, HOURS), the project they are working on (column C, PROJECT), and their specific task (column D, TASK).

I would like to populate the second spreadsheet with the same information, but only where the PROJECT equals "Project X" and the Task equals either "Task 1" or "Task 2."

I know how to do this without scripts in the source document, but Google spreadsheets does not allow VLookup, or any other lookup function that I am aware of, to lookup values across separate documents. Importing the source data to the second spreadsheet using ImportRange does not work in this case because the source data is too large (ultimately, this script will have to be altered to look at several source documents since there is too much data for one spreadsheet).

I am sorry I have no code. My attempts to solve this problem on my own have gotten me nowhere.

Thank you!


回答1:


Short answer: you cant, psrticularly if the data is large. you might be able to do it writting the formula with script (using vlookup or query) but that has limitations, for example you have to take turns if more than one script runs simultaneously. Another more robust way is to use urlfetch with the spreadsheets api. Use the list rows api with a "q" filter. That works well even with simultaneous scripts.




回答2:


This is a slightly old thread, but there are so few examples of how to do this that I thought one more couldn't hurt. You can pass an SQL statement into the querySpreadsheet() function below, something like:

var querySQL = "SELECT A, B, C, D WHERE C = 'Project X' AND (D = 'Task 1' OR D = 'Task 2')";

You can then drop the array of data returned by the function into your target sheet like so:

var aryValues = querySpreadsheet(querySQL);
SpreadsheetApp.getActiveSpreadsheet().getRangeByName('OUTPUT_TOPLEFT_CELL').offset(0, 0, aryValues.length, 4).setValues(aryValues);

These functions are almost good to go, just substitute your information where indicated. Credit to Mogsdad for the original of this script here: Using QUERY URL to group data before sending to Google Apps Script dashboard

function querySpreadsheet(querySQL){ 

try {  

  //you can find both these items in the url of your spreadsheet
  var ssKey = 'DOCUMENT KEY OF THE DATA SOURCE SPREADSHEET INSIDE THESE QUOTES - long code ends with "&" starts with "key="';
  var gId = 'INDEX OF THE SHEET CONTAINING THE DATA - starts with "gid="';
  var query = encodeURIComponent(querySQL);

  var url = 'http://spreadsheets.google.com/tq?key=%KEY%&gid=%GID%&tq=%QUERY%'
  .replace('%KEY%',ssKey)
  .replace('%QUERY%',query)
  .replace('%GID%',gId);

  var response = UrlFetchApp.fetch(url);
  var content = response.getContentText();    
  var jsonContent = getJSON(content);

  var numCols = jsonContent.table.cols.length;
  var numRows = jsonContent.table.rows.length;

  // Decode objectContent into a two-dimensional array.
  var mydata = [];
  // Then data rows
  for (var row = 0; row < numRows; row++) {
    mydata[row] = [];
    for (var col = 0; col < numCols; col++ ) {
      mydata[row].push(jsonContent.table.rows[row].c[col].v);
    }
  }

  return mydata;

}  catch(error) {
  Logger.log('querySpreadsheet: ERROR - ', error);
};    
};

//the content text format is not uniform; try 2 methods to return a JSON object 
function getJSON(contentText) {

try {  

  var regex = /.*google.visualization.Query.setResponse\((.*)\)/g    
  var ret = eval('('+ regex.exec(contentText)[1] +')');
  return ret;

}  catch(error) {

  var ret = regex.exec(contentText)[1];
  var ret = Utilities.jsonParse(ret);
  return ret;

}; 
};


来源:https://stackoverflow.com/questions/19171591/how-to-perform-a-lookup-filter-across-google-spreadsheets-using-google-apps-scri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!