Google表格脚本:在电子表格中按字母顺序对表格进行排序

时间:2017-02-24 13:34:33

标签: sorting google-sheets google-sheets-api

我遇到了一个旧的帮助表单,其中使用javascript在电子表格中script is shared to organize sheets。脚本如下:

function copyAllSheetsToAnotherSpreadsheetInAlphabeticalOrder() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sourceName = ss.getName();
  var sourceSheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
  var targetName = "Sheets alphabetized - Copy of " + sourceName;
  var sheetNumber, sourceSheet, sheetName;
  var sheetAlphaArray = new Array();

  // create a new empty target spreadsheet
  var targetSpreadsheet = SpreadsheetApp.create(targetName);
  var targetUrl = targetSpreadsheet.getUrl();

  // iterate through all sheets in the source spreadsheet to collect their names and numbers
  for( sheetNumber = 0; sheetNumber < sourceSheets.length; sheetNumber++) {
    sheetAlphaArray[sheetNumber] = new Array(2);
    sheetName = sourceSheets[sheetNumber].getName();
    // we will sort the array by the sheet name so it needs to be the first element
    sheetAlphaArray[sheetNumber][0] = sheetName;
    // need to keep track of sheet numbers so that we can find the sheets
    // in alphabetical order from the sourceSheets array, place it in the second element
    sheetAlphaArray[sheetNumber][1] = sheetNumber;
  }

  // sort the sheet names array in ascending alphabetic order
  sheetAlphaArray.sort();

  // iterate through all sheets in the source spreadsheet by sheet name in alphabetic order
  //
  // new sheets are always added to the first position, so the sheets need to be added
  // last sheet first, first sheet last, otherwise they would appear in reverse order
  for( sheetNumber = sourceSheets.length - 1; sheetNumber >= 0; sheetNumber-- ) {

    // copy next sheet in reverse alphabetical order from the source spreadsheet to target spreadsheet
    sourceSheet = sourceSheets[ (sheetAlphaArray[sheetNumber][1]) ];
    sourceSheet.copyTo(targetSpreadsheet);
  }

  // done, tell user where to find the new spreadsheet
  Browser.msgBox("Spreadsheet copied with sheets in alphabetical order. " +
                 "Target name: " + targetName + ". " +
                 "Target URL: " + targetUrl ) + ".";
}

在运行脚本之后,几个页面似乎按字母顺序排序,但在滚动排序的页面后,有一些页面尚未排序。前15个排序后跟21个未排序。

enter image description here

1 个答案:

答案 0 :(得分:0)

我意识到这个问题与JavaScript的from random_words import RandomWords import requests import urllib.request import re from urllib.parse import urlparse as url from threading import Thread from tkinter import * def update(new): var.set(new) pencere.update_idletasks() def yeni1(): pencere2 = Tk() pencere2.geometry("1000x600+300+300") pencere2.tk_setPalette("black") baslik = pencere2.title("Tesekkürler") var2 = StringVar() var2.set(""" DİKKAT! Bu program Hacknology tarafından kodlanmıştır ve izinsizce paylaşılması yasaktır. İyi kullanımlar dilerim.""") var3 = StringVar() var3.set("""THX""") etiket2 = Label(pencere2, textvariable=var2, fg="blue", bg="#000000", font="Helvetica 25 bold") etiket2.pack(side=TOP) etiket3 = Label(pencere2, textvariable=var3, fg="orange", bg="#000000", font="Helvetica 25 bold") etiket3.pack(side=TOP) mainloop() def kodlar(): print("Hello world") pencere = Tk() pencere.geometry("800x600+100+100") ##yeni_pencere_buton= Button(text="ODIN", command=yeni) ##yeni_pencere_buton.pack() buton1 = Button(text="Teşekkür ve Hakkında", command=yeni1) buton1.pack() baslik = pencere.title("ODIN GIRIS") var = StringVar() var.set("ODIN PROJECT ") pencere.tk_setPalette("black") etiket = Label(pencere,textvariable=var, fg="blue", bg="#000000", font="Helvetica 25 bold") etiket.pack(side=TOP) 函数有关,它对大写单词的处理方式与小写不同。所以我修改了:

.sort()

是:

sheetName = sourceSheets[sheetNumber].getName();

进行此更改后,新的已排序电子表格似乎已按顺序排列。