簡體   English   中英

我想為谷歌工作表創建一個腳本,它將制作一張工作表的多個副本並將新工作表放在活動工作表的左側

[英]I want to create a script for google sheets that will make multiple copies of a sheet and place the new sheets to the left of the active sheet

我對使用谷歌腳本很陌生。 我有一個帶有按日期標題的選項卡的電子表格。 我發現以下腳本可以讓我創建可變數量的重復工作表。 該腳本還將通過將日期更改為下一個日期來自動重命名新工作表。 唯一的問題是所有新工作表都添加到文檔的最右側。 我希望將每個副本放在活動工作表的左側。 因此,如果我有一個標題為 21 年 1 月 3 日的選項卡,並且我制作了該選項卡的 3 個副本,那么新的選項卡將從 21 年 1 月 6 日從左到右讀取; 21 年 1 月 5 日; 21 年 1 月 4 日。 這是我正在使用的腳本。 我必須禁用“啟用 Chrome V8 運行時”才能使該腳本正常工作。

function duplicatesheet() {
  var as = SpreadsheetApp.getActiveSpreadsheet(); // active spreadsheet
  var s = as.getActiveSheet(); // first sheet object
  var dateCell = "A6:N6"; // cell containing first date
  var N = 5; // number of copies to make

  var startDate = new Date(s.getRange(dateCell).getValue()); // get the date stored in dateCell
  var day = startDate.getDate(); // extract the day
  var month = startDate.getMonth(); // extract the month
  var year = startDate.getFullYear(); // extract the year

  // loop over N times
  for (var i = 0; i < N; i++) {
    var asn = s.copyTo(as); // make a duplicate of the first sheet
    var thisSheetDate = new Date(year, month, day+(i+1)); // store the new date as a variable     temporarily

    asn.getRange(dateCell).setValue(thisSheetDate); // writes the date in cell "B3"
    asn.setName(Utilities.formatDate(thisSheetDate, undefined, "MM/dd/yy")); // sets the name of     the new sheet
  }
}

解釋:

正如另一個答案所解釋的,您可以使用insertSheet(sheetName, sheetIndex, options) 此答案解釋了如何將 function 合並到您的代碼中,以及如何使您的代碼更快、更高效。

  • 在您的代碼dateCell是一系列單元格"A6:N6" ,但您只需要一個值,即A6 因此,只需將其更改為A6

  • 您不需要創建您價值的日期 object。 如果您的工作表中的A6包含一個日期值,那么GAS會正確提取它。

  • 您不需要迭代地構建自己的日期對象。 您可以使用:

     startDate.setDate(startDate.getDate()+1);

    這將使日期增加一天。 通過這種方式,您可以擺脫daymonthyear變量。

  • 我不確定您為什么將undefined作為Utilities.formatDate的第二個參數,也不確定為什么您的腳本編輯器沒有出錯。 但是您可能想改用as.getSpreadsheetTimeZone()

  • 要正確地將工作表插入到for循環內活動工作表的左側,您需要一個像sindex-N+1這樣的索引表達式。 使用+1是因為getIndex1開始計數。 也就是說,對於文檔中的第一張表, getIndex將返回1 但是insertSheet中的sheetIndex參數接受從0開始的索引。

解決方案:

function duplicatesheet() {
  var as = SpreadsheetApp.getActiveSpreadsheet(); 
  var s = as.getActiveSheet(); 
  var sindex = s.getIndex();
  var dateCell = "A6"; 
  var N = 2; 
  var startDate = s.getRange(dateCell).getValue();
  // loop over N times
  for (var i = 0; i < N; i++) {
    startDate.setDate(startDate.getDate()+1);
    var startDateStr = Utilities.formatDate(startDate, as.getSpreadsheetTimeZone(), "MM/dd/yy")
    var asn=as.insertSheet(startDateStr, sindex-N+1, {template: s});
    asn.getRange(dateCell).setValue(startDate); 
  }
}

這似乎很容易做到:

function myFunction() {
  let ss = SpreadsheetApp.getActiveSpreadsheet();
  let sheet = SpreadsheetApp.getActiveSheet(); 
  
  let index = sheet.getIndex();

  ss.insertSheet("New Sheet", index - 1, {template: sheet, });
}


執行此代碼后,您的工作表在執行后應如下所示:

在此處輸入圖像描述

參考

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2025 STACKOOM.COM