[英]How to copy data from source range and paste only rows with content to a separate target workbook sheet, sorted by date values
Seeking a script that will copy data from source sheet range B4:C and paste to a separate target workbook columns H4:I only rows with content in one or both cells (ie. discarding row 7 of the top table below), sorted by source column C date values latest to earliest, as demonstrated in the tables below:寻找一个脚本,该脚本将从源工作表范围 B4:C 复制数据并粘贴到单独的目标工作簿列 H4:I仅在一个或两个单元格中包含内容的行(即丢弃下表的第 7 行),按源排序列 C 日期值最新到最早,如下表所示:
Below is best I can make it work, though it doesn't omit empty rows, sort by date, and only evaluates a static source range.下面是最好的,我可以让它工作,虽然它不会省略空行,按日期排序,并且只评估 static 源范围。 And it takes kind of a long time to process (~2 minutes, but it's not that big of a deal), so I'm hoping a proper script as suggested by the Community might improve that aspect as well:
而且处理需要很长时间(约 2 分钟,但这没什么大不了的),所以我希望社区建议的适当脚本也可以改善这方面:
function Import() {
var cc = SpreadsheetApp.getActiveSheet();
var ss = SpreadsheetApp.openById('sheetID').getSheetByName('tabname');
var data = ss.getRange("B4:C8723").getValues();
cc.getRange("H4:I").clearContent();
cc.getRange("H4:I8723").setValues(data)
}
The desired script is, in effect, the following formula:所需的脚本实际上是以下公式:
=query(importrange(sheet,tab!range),"select B,C where B is not null order by C desc")
Hope the request is clear;希望要求是明确的; thanks for the help.
谢谢您的帮助。
I believe your goal is as follows.我相信你的目标如下。
=query(importrange(sheet,tab,range),"select B,C where B is not null order by C desc")
to Google Apps Script.=query(importrange(sheet,tab,range),"select B,C where B is not null order by C desc")
为 Google Apps 脚本。 In this case, how about the following modification?在这种情况下,如何进行以下修改?
cc.getRange("H4:I").clearContent();
cc.getRange("H4:I8723").setValues(data)
var values = data.filter(([b]) => b != "").sort(([,c1], [,c2]) => c1 > c2 ? -1 : 1);
cc.getRange("H4:I").clearContent();
cc.getRange("H4:I" + (values.length + 3)).setValues(values);
function Import() {
var dss = SpreadsheetApp.getActive();
const dsh = dss.getActiveSheet();
var sss = SpreadsheetApp.openById('sheetID');
const ssh = sss.getSheetByName('tabname');
var vs = ssh.getRange(4,2,ssh.getLastRow() -3,2).getValues().filter(r => r[0] && r[1]);
dsh.getRange(4,8,dsh.getLastRow() - 3, 2).clearContent();
dsh.getRange(4,8,vs.length,vs[0].length).setValues(vs)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.