简体   繁体   English

如何从源范围复制数据并仅将包含内容的行粘贴到单独的目标工作簿工作表中,按日期值排序

[英]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 日期值最新到最早,如下表所示:

  • Data source is constantly being added to and rearranged, so this is a growing range;数据源不断被添加和重新排列,所以这是一个不断增长的范围; not a static range dimension as is defined in my below script不是我下面脚本中定义的 static 范围尺寸

在此处输入图像描述

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.我相信你的目标如下。

  • You want to convert the formula of =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?在这种情况下,如何进行以下修改?

From:从:

cc.getRange("H4:I").clearContent();
cc.getRange("H4:I8723").setValues(data)

To:至:

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);
  • In this modification, the filtered values are sorted and put them to the destination sheet.在此修改中,对过滤后的值进行排序并将它们放置到目标工作表中。

References:参考:

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.

相关问题 从一张纸复制数据,将日期附加到行上并粘贴 - Copy data from one sheet, append date to rows and paste 复制一个范围并仅将具有值和图像的行粘贴到另一个工作表中下一个空白行的谷歌工作表中? - Copy a range and paste only rows with values & images into another sheet at the next blank row in the google sheets? 复制一个范围并仅将带有值的行粘贴到下一个空白行的另一个工作表中? - Copy a range and paste only rows with values into another sheet at the next blank row? 尝试将数据从源 Google 工作表的最后一行复制并粘贴到目标工作表的最后一行时似乎出现错误 - Seem to be getting an error when trying to copy and paste data from last row of the source google sheet to the target sheet last row 从范围复制单元格值并将其粘贴到另一个工作表的单行中 - To copy Cell values from a range and paste it in a single row of another sheet 将源工作表中的数据与目标工作表进行比较,并使用 Google Apps 脚本将缺失的行复制到目标工作表 - Compare data in source sheet with target sheet and copy missing rows to target sheet with Google Apps Script 如何从一个工作表中复制有效范围并将数据粘贴到Google工作表中同一工作簿中不同工作表中的特定范围内? - How to copy active range from one sheet and pasting the data to a specific range in a different sheet within the same workbook in google sheets? 仅从一张纸复制值并粘贴到另一张纸上 - copy only values from one sheet and paste to another 需要脚本来遍历股票代码列表,复制生成的 output,并将多行的 output 粘贴到工作簿中的单独工作表中 - Need script to loop through stock ticker list, copy resulting output, and paste the output of multiple rows to separate sheet within workbook Google App Script - 如何将数据范围的粘贴值(仅非空单元格)从电子表格复制到另一个电子表格 - Google App Script - how to Copy Paste values ( only non empty cells) of a data range with , from a Spreadsheet to another Spreadsheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM