方法“moveActiveSheet”在Google Apps脚本(JavaScript)中无法正常工作 [英] Method "moveActiveSheet" doesn't work as expected in Google Apps Script (JavaScript)

查看:177
本文介绍了方法“moveActiveSheet”在Google Apps脚本(JavaScript)中无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google Apps脚本编写脚本。



目标:允许用户按每张表名称中的日期对Google电子表格中的工作表/选项卡进行排序。

  function sorter(){
var sp = SpreadsheetApp.getActiveSpreadsheet();
var sheets = sp.getSheets();
var sheet_metadata = get_and_sort_sheet_metadata(sheets)
for(var j = 0; j< sheet_metadata.length; j ++){
sp.setActiveSheet(sp.getSheetByName(sheet_metadata [j] .name ));
sp.moveActiveSheet(j + 1);


这是sheet_metadata内容的一个例子:

  [{date = false,name = A,index = 1},{date = false,name = B,index = 2}, {date = 1.391058E12,name = ARCHIVE:xname0 name:1/30/2014 ID:145952,index = 8},{date = 1.42173E12,name = ARCHIVE:zname1 name:1/20/2015 ID:1459527232,index = 7},{date = 1.4543892E12,name = ARCHIVE:wname3 name:2/2/2016 ID:145952723299,index = 6},{date = 1.4544756E12,name = ARCHIVE:dname4 name:2/3/2016 ID :date = 1.4546484E12,name = ARCHIVE:qname6 name:2 / 5/2016 ID:1459500,index = 3}] 

日期值是.getTime ()。名称是工作表的名称。索引是表单的原始索引。此数组中对象的顺序是工作表应该放入的顺序。



sheet_metadata中的对象始终按正确的顺序排列。但是,for循环(请参阅上文)无法按正确的顺序放置页面。换句话说,纸张的最终订单与sheet_metadata中的工作表数据的顺序不一致。



为什么这不起作用?

解决方案

前几天我遇到了同样的问题,我找到了一种方法来处理这段代码:

在这个例子中,

profSheets是一个数组表格名称按我想要的方式排序。

  var pos = ss.getNumSheets(); (var s = 0; s< profSheets.length; s ++){
var sh = ss.setActiveSheet(ss.getSheetByName(profSheets [s]));

SpreadsheetApp.flush();
Utilities.sleep(200);
ss.moveActiveSheet(pos);
SpreadsheetApp.flush();
Utilities.sleep(200);
}

我将应该在第一个位置的工作表放到最后一个位置,然后下一个位于最后一个位置,依此类推直到列表中的姓氏。最后,第一个实际上是第一个。 (我希望这是可以理解的; - )

I am writing a script in Google Apps Script.

The goal: allow users to sort the sheets/tabs in a Google spreadsheet by the date in the name of each sheet.

function sorter() {
  var sp = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = sp.getSheets();
  var sheet_metadata = get_and_sort_sheet_metadata(sheets)
  for( var j = 0; j < sheet_metadata.length; j++ ) {
    sp.setActiveSheet(sp.getSheetByName(sheet_metadata[j].name));
    sp.moveActiveSheet(j + 1);    
  }
}

This is an example of sheet_metadata's content:

[{date=false, name=A, index=1}, {date=false, name=B, index=2}, {date=1.391058E12, name=ARCHIVE: xname0 name: 1/30/2014 ID:145952, index=8}, {date=1.42173E12, name=ARCHIVE: zname1 name: 1/20/2015 ID:1459527232, index=7}, {date=1.4543892E12, name=ARCHIVE: wname3 name: 2/2/2016 ID:145952723299, index=6}, {date=1.4544756E12, name=ARCHIVE: dname4 name: 2/3/2016 ID:145952723266, index=5}, {date=1.454562E12, name=ARCHIVE: qname5 name: 2/4/2016 ID:1459500, index=4}, {date=1.4546484E12, name=ARCHIVE: qname6 name: 2/5/2016 ID:1459500, index=3}]

The date value is the result of .getTime(). The name is the sheet's name. The index is the original index of the sheet. The order of objects in this array is the order the sheets should be in.

The objects in sheet_metadata are always in the correct order. However, the for loop (see above) fails to place the sheets in the correct order.

In other words, the order the sheets end up in does not correspond to the order of the sheet data in sheet_metadata.

Why doesn't this work?

解决方案

I have had the same problem a few days ago and I found a way to get it working with this code :

in this example profSheets is an array of sheet names sorted the way I want.

  var pos = ss.getNumSheets();
  for(var s=0 ; s < profSheets.length ; s++){
    var sh = ss.setActiveSheet(ss.getSheetByName(profSheets[s]));
    SpreadsheetApp.flush();
    Utilities.sleep(200);
    ss.moveActiveSheet(pos);
    SpreadsheetApp.flush();
    Utilities.sleep(200);
  }

I place the sheet that should be in first position to the last position, then the next one at the last position and so on up to the last name in the list. At the end, the first one is actually in the first place. (I hope this is understandable ;-)

这篇关于方法“moveActiveSheet”在Google Apps脚本(JavaScript)中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆