簡體   English   中英

Google Sheets:如何制作一個宏/腳本,對於所有選定的行,在下面插入兩行,然后復制並粘貼原始內容?

[英]Google Sheets: How to make a macro/script that, for all selected rows, inserts two rows beneath, then copies and pastes the content from the original?

見標題! 我不知道在代碼或其他方面我在做什么,但我在 Google 表格中工作。 我有大張紙,基本上,我需要為每一行創建兩個副本。 我為足夠多的不同工作表執行此操作,以便能夠運行為我執行此操作的宏/腳本真的很有幫助。 問題是,當我嘗試使用宏記錄器時,它只是插入,然后復制和粘貼我記錄宏的“測試”行。 我需要一個宏來復制我選擇的所有行。 謝謝!!

你可以這樣做:

function copyThreeTimes() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const range = ss.getRangeByName('source');
  const values = range.getValues();
  let destinationRange = ss.getRangeByName('destination');
  const destinationSheet = destinationRange.getSheet();
  const destinationValues = []
  for (let i = 1; i < values.length; i++){
    if (values[i][0]){
      for (let j = 0; j < 3; j++)
        destinationValues.push(values[i]);
    }
  }
  const maxRows = destinationSheet.getMaxRows()
  destinationSheet.insertRowsAfter(maxRows, destinationValues.length);
  destinationRange = destinationSheet.getRange(maxRows + 1, 1, destinationValues.length, 3);
  destinationRange.setValues(destinationValues);
}

為您的范圍使用命名范圍。 然后獲取值並跳過標題。 復制 3 倍的值,然后重塑目標范圍並粘貼該值。

這是我的示例電子表格

暫無
暫無

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

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