簡體   English   中英

如何將新創建的工作表設置為活動工作表? 谷歌表格

[英]How to set the newly created sheet as the active sheet? Google sheets

我有一個問題,由於無法切換到由 function copyWithProtections 生成的新創建的工作表,我必須單獨運行每個腳本,所以當下一個 function ClearValueAftercreatingnewsheet 運行時,它在活動工作表上運行而不是新生成的工作表,有沒有辦法讓新創建的工作表成為活動工作表?

 /* CAUTION: COPY WITH PROTECTION SHOULD BE RUNNED FIRST THEN CLEARVALUEAFTERCREATING NEW SHEET AFTER MAKING SURE THAT YOU MANUALLY CHANGED THE ACTIVE SHEET TO THE NEW SHEET WITH THE NUMBER */ //Copies with protection function copyWithProtections(){ const sh = SpreadsheetApp.getActiveSpreadsheet(); const ss = sh.getSheetByName("Mar22"); const prot = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE) let nSheet = ss.copyTo(sh).setName(sh.getNumSheets()-1); let p; for (let i in prot){ p = nSheet.getRange(prot[i].getRange().getA1Notation()).protect(); p.removeEditors(p.getEditors()); if (p.canDomainEdit()) { p.setDomainEdit(false); } } } //Clears Values of new sheets function ClearValueAftercreatingnewsheet() { var spreadsheet = SpreadsheetApp.getActive(); spreadsheet.getRange('A2:P144').activate().clear({contentsOnly: true}); spreadsheet.getRange('Z5').activate(); spreadsheet.getCurrentCell().setValue(''); spreadsheet.getRange('Z8').activate(); spreadsheet.getCurrentCell().setValue(''); spreadsheet.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true}); spreadsheet.getRange('A2:Q270').activate(); spreadsheet.getActiveRangeList().setBackground('#deeaf6'); spreadsheet.getRange('A2:R270').activate(); spreadsheet.getActiveRangeList().setBorder(true, true, true, true, true, true, '#000000', SpreadsheetApp.BorderStyle.SOLID); };

您可以從工作表名稱設置活動工作表。 通過在單個 function 中制作腳本,您可以使用新創建的 output 工作表nSheet並使用getSheetName()獲取它的名稱。 然后您可以在清除值部分引用它。 請嘗試使用以下代碼:

function copyAndClear() {

  //Copies with protection
  const sh = SpreadsheetApp.getActiveSpreadsheet();
  const ss = sh.getSheetByName("Mar22");
  const prot = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE)

  let nSheet = ss.copyTo(sh).setName(sh.getNumSheets() - 1);
  let p;

  for (let i in prot) {
    p = nSheet.getRange(prot[i].getRange().getA1Notation()).protect();
    p.removeEditors(p.getEditors());
    if (p.canDomainEdit()) {
      p.setDomainEdit(false);
    }
  }

  //Set the newly created sheet name in a variable to be used for reference
  var nSheetName = nSheet.getSheetName();

  //Clears Values of new sheets
  var spreadsheet = sh.getSheetByName(nSheetName);
  spreadsheet.getRange('A2:P144').activate().clear({contentsOnly: true});
  spreadsheet.getRange('Z5').setValue('');
  spreadsheet.getRange('Z8').setValue('');
  spreadsheet.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});
   spreadsheet.getRange('A2:Q270').setBackground('#deeaf6');
  spreadsheet.getRange('A2:R270').setBorder(true, true, true, true, true, true, '#000000', SpreadsheetApp.BorderStyle.SOLID);
};

我還簡化了您的代碼以避免冗余並縮短運行時間,因為根據您的代碼,如果您可以直接在一行中設置它,則不需要.activate()

讓我知道這個是否奏效!

您也可以通過 ID 設置活動工作表,但代碼會更長。 這是設置活動工作表的參考: https://spreadsheet.dev/activate-sheet-in-google-sheets-using-google-apps-script

編輯:我將.activate()保留在第一行

spreadsheet.getRange('A2:P144').activate().clear({contentsOnly: true});

至於出於某種原因,如果我直接將其設置為

spreadsheet.getRange('A2:P144').clear({contentsOnly: true});

沒有.activate()它也會清除原始工作表。

暫無
暫無

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

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