[英]Filtering google sheets import data before import
I have a script that imports data from a google sheet.我有一个从谷歌表格导入数据的脚本。 Before importing the data into the new sheet I would like to filter it.
在将数据导入新工作表之前,我想对其进行过滤。 In Column2 are our Cost centers listed and all cost centers starting with '41' should be filtered and imported to the sheet.
在 Column2 中列出了我们的成本中心,所有以“41”开头的成本中心都应该被过滤并导入到工作表中。 Right now I'm a little bit blocked in saying filtering by column2 where the string startswith '41'.
现在我在说按字符串以“41”开头的 column2 过滤时有点受阻。
// Gets the active sheet.
var sheet = SpreadsheetApp.getActiveSheet();
// Gets a different spreadsheet from Drive using
// the spreadsheet's ID.
var employeeActuals = SpreadsheetApp.openById(
"1yL_0eB9b6CQLOshjPglDA-MnP2HZdLeIrKh4DO-qN0c"
);
// Gets the sheet, data range, and values of the
// spreadsheet stored in employeeActuals.
var employeeActualsSheet = employeeActuals.getSheetByName("Overview")
var range = employeeActualsSheet.getDataRange();
var rangeValues = range.getValues();
var databasis = rangeValues.filter(function(Item){return Item[1] === String().startsWith(["41"]) })
Logger.log(databasis); ```
Try something like this:尝试这样的事情:
function myfunk() {
const sheet = SpreadsheetApp.getActiveSheet();
const ss = SpreadsheetApp.openById(gobj.globals.testsourceid);
const esh = ss.getSheetByName("Sheet1")
const range = esh.getDataRange();
const vs = range.getValues();
let data = vs.filter(e => e[0].startsWith('17'))
Logger.log(data);
}
Data:数据:
COL1![]() |
COL2![]() |
COL3![]() |
---|---|---|
10,1 ![]() |
10,2 ![]() |
10,3 ![]() |
11,1 ![]() |
11,2 ![]() |
11,3 ![]() |
12,1 ![]() |
12,2 ![]() |
12,3 ![]() |
13,1 ![]() |
13,2 ![]() |
13,3 ![]() |
14,1 ![]() |
14,2 ![]() |
14,3 ![]() |
15,1 ![]() |
15,2 ![]() |
15,3 ![]() |
16,1 ![]() |
16,2 ![]() |
16,3 ![]() |
17,1 ![]() |
17,2 ![]() |
17,3 ![]() |
18,1 ![]() |
18,2 ![]() |
18,3 ![]() |
19,1 ![]() |
19,2 ![]() |
19,3 ![]() |
20,1 ![]() |
20,2 ![]() |
20,3 ![]() |
output: output:
12:09:45 PM Notice Execution started
12:09:45 PM Info [[17,1, 17,2, 17,3]]
12:09:47 PM Notice Execution completed
The getValues()
method returns a two dimensional string so in order to filter out the values, you will also need a loop in order to the filtering properly: getValues()
方法返回一个二维字符串,因此为了过滤掉值,您还需要一个循环才能正确过滤:
var filterVals = [];
for (let i = 0; i < rangeValues.length; i++) {
var values = rangeValues[i].filter(element => element.toString().startsWith('41'));
filterVals.push(values);
}
let databasis = filterVals.filter(element => element.toString() != "")
Logger.log(databasis);
You can filter data before importing.您可以在导入前过滤数据。 Gsheets allow doing that with a query formula or a filter formula.
Gsheets 允许使用查询公式或过滤公式来做到这一点。 The flow will be employeeActualsSheet -> filtered employeeActualsSheet -> import data.
流程将是 employeeActualsSheet -> 已过滤的 employeeActualsSheet -> 导入数据。 Question - what is your data destination?
问题 - 您的数据目的地是什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.