Is there a way to sort this Google scripts function numerically/opposite direction?

泄露秘密 提交于 2020-03-04 15:34:05

问题


Here is the script that I'm using to pull all the sheet names of a Google Sheet Document into a list on a front sheet. It is working; however, is there a way to sort this the opposite way? I have the sheets named by date with the oldest date as the furthest sheet and the newest sheet the closest to the front. So it is sorting the newest date first but I would like it to sort with the oldest date first. Also, is there a way to make this function dynamic so it updates as new sheets are added or if the sheet names change?

function sheetnames() {
  var out = new Array()
  var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();


for (var i=0 ; i<sheets.length ; i++) {
  Logger.log(String(sheets[i].getSheetId()));
 // var sheetId = sheets.getSheetId()
  out.push( [ sheets[i].getName() ] ) 
}

return out 
}

回答1:


try:

function SHEETLIST() {
try {
  var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets()
  var out = new Array( sheets.length+1 ) ;
  out[0] = [ "♥♥" , "#GID" ];
  for (var i = 1 ; i < sheets.length+1 ; i++ ) out[i] = 
  [sheets[i-1].getName() , sheets[i-1].getSheetId() ];
  return out
}
catch( err ) {
  return "#ERROR!" }}

and formula:

=ARRAYFORMULA(QUERY(IFERROR(SPLIT(INDEX(SHEETLIST(),,1)&"♦"&ROW(A:A), "♦")), 
 "select Col1 where Col1 <> '♥♥' order by Col2 desc", 0))



回答2:


Use Array.reverse():

function sheetnamesreverse() {
  return SpreadsheetApp.getActive()
    .getSheets()
    .map(s => [s.getName()])
    .reverse();
}



回答3:


You could use an onChange Installable Trigger, but take into consideration its Restrictions. In this way, the code I will be providing you, it will run every time you add a new sheet or you edit a sheet's name:

// Installable Trigger
function onChange() {
  var dates = sheetNames();
  Logger.log(dates);
}

function sheetNames(){
  try{
    // Get all sheets and then return
    return SpreadsheetApp.getActiveSpreadsheet()
             .getSheets()
             // Get a new array only with the sheet names
             .map(function(sheet){ return sheet.getName() })
             // Sort the array from the oldest date to the newest date
             .sort(
               function (sheet1, sheet2){
                 return new Date(sheet2) - new Date(sheet1);
               });
  } catch(e){
    Logger.log("Not a date");
    return e;
  }
}

Now, for setting up the installable trigger, do the following:

1) Go to your Apps Script project

2) Click Edit->Current project's triggers

3) Click "+ Add Trigger"

4) Select :

Choose which function to run ->

Select event source-> From spreadsheet

Select event type -> On change

5) Click Save




回答4:


Sorry I answered the wrong question.



来源:https://stackoverflow.com/questions/60213007/is-there-a-way-to-sort-this-google-scripts-function-numerically-opposite-directi

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