简体   繁体   English

每天使用Google表格中的Google Script按价格排序

[英]Sort by price daily with Google Script within Google Sheets

I am looking to be pulling in some data into a Google Sheet from an external source at 1am daily. 我希望每天凌晨1点从外部来源将一些数据提取到Google表格中。 The amount of products imported will vary. 进口产品的数量会有所不同。

I then wish to sort the data by price after the data has finished uploading, this will be around 1.05am. 然后,我希望在数据上传完毕后按价格对数据进行排序,这将是上午1.05左右。

I see it is possible to run functions for Google Sheets on a timed basis. 我看到可以定时运行Google表格的功能。

What script would I need to sort the data by price, so the cheapest items is row 2 after running a script every morning. 我需要哪种脚本按价格对数据进行排序,所以最便宜的项目是每天早上运行脚本后的第2行。

To do this manually in Google Sheets I would do - *Highlight rows 2 to , Data, Sort Range, Sort by column I, A - Z 要在Google表格中手动执行此操作,我需要-*突出显示第2行到,数据,排序范围,按列I,A-Z排序

How would this translate into a function? 这将如何转换为功能? the timed function look easy enough to do as a trigger (although a set time doesn;t seem possible only an hour range) 定时功能看起来很容易触发(尽管设定的时间似乎不可能;只是一个小时的范围)

Here is the shared sheet 这是共享的工作表

https://docs.google.com/spreadsheets/d/1hRW92xesCZzrTRU8DzrdJ_XtrQoJbbNhA3nVT452 apE/edit?usp=sharing https://docs.google.com/spreadsheets/d/1hRW92xesCZzrTRU8DzrdJ_XtrQoJbbNhA3nVT452 apE / edit?usp = sharing

SOLUTION - Thanks to Ed & Cooper 解决方案-感谢Ed&Cooper

First, freeze the first row manually. 首先,手动冻结第一行。 (Search for freeze) https://support.google.com/docs/answer/54813?hl=en (搜索冻结) https://support.google.com/docs/answer/54813?hl=zh_CN

function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// Sorts the sheet by the column I - the ninth across - , in ascending order 
sheet.sort(9);
}

Set this function to run one hour after you data is available: 将此功能设置为在数据可用后一小时运行:

function sortPrice() {
  var ss=SpreadsheetApp.openById("1XHOlmI6LA4jvnh1YXossBrbrnIOdyRmvO2warz1vl9s")
  var s=ss.getSheetByName("4cat").activate()
  var lr =s.getLastRow()
  var lc= s.getLastColumn()
  var rng=s.getRange(2, 1, lr, lc)//get range to sort
  var sort= rng.sort({column: 9, ascending: true}) //sort on column I
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM