简体   繁体   English

Google Sheets 在将其合并到另一个工作表时保存工作表的样式

[英]Google Sheets save the style of sheet while merging it to another sheet

I need to save the styles of the sheets when I combine them into a mainsheet.当我将它们组合成一个主表时,我需要保存这些表的样式。 Here is the code:这是代码:

function myFunction() 
 {
   var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
   var newSheet = activeSpreadsheet.getSheetByName("MainSheet");
   
   if (newSheet != null) {
        activeSpreadsheet.deleteSheet(newSheet);
    }

   newSheet = activeSpreadsheet.insertSheet();
   newSheet.setName("MainSheet");
   const ss = SpreadsheetApp.getActiveSpreadsheet();
   const allSheets = ss.getSheets();
   const allSheets_names=allSheets.map(sheet=>sheet.getSheetName())  
   const dataRange = "A1:M";
   const checkRange = "A1:A";
   const neededSheets = ["Cats", "Dogs"];
   const filteredListOfSheetsNames = [];       
  neededSheets.forEach(function(ns){  
    var i = neededSheets.indexOf(ns);
    filteredListOfSheetsNames[i]=[];
    allSheets_names.forEach( (as,index) => {                            
      if (as.indexOf(ns)>-1){
        filteredListOfSheetsNames[i].push(as);
        }
      }
    )
    const filteredListOfSheets  =  filteredListOfSheetsNames[i].map(name =>ss.getSheetByName(name));  
    var array = [];
    filteredListOfSheets.forEach(function(sheet){var values = sheet.getRange(1,1,sheet.getLastRow(),13).getValues(); array.push(values);});

    array = [].concat.apply([],array);
    if(array.length > 0){
      newSheet.getRange(1,i*13+1, array.length, array[0].length).setValues(array);
      }
    }
  )
}

Currently, I get all the information correctly.目前,我正确地获得了所有信息。 On one side of the mainsheet I get sheets that contain a word "dog" in them and on another side of the mainsheet I get the sheets that contain a word "cat".在主表的一侧,我得到了包含“狗”一词的表,而在主表的另一侧,我得到了包含“猫”一词的表。 The issue is that the font, colors disappear after I combine all the sheets.问题是字体、颜色在我组合所有工作表后消失了。 How should I keep the styles when combining the sheets?组合床单时如何保持样式?

Here is an example of the MainSheet on how it should look like, but it should also take all of the styles from the sheets.这是 MainSheet 的一个示例,说明它应该是什么样子,但它也应该采用工作表中的所有样式。 MainSheet 示例

You can use copyTo to copy the formatting from one sheet to another.您可以使用copyTo将格式从一张纸复制到另一张纸。 See documentation here .请参阅此处的文档。

Here is a minimal example based off of your script that copies the formatting to mainSheet from sheets Cats and Dogs .这是一个基于脚本的最小示例,该示例将格式从工作表CatsDogs复制到mainSheet This script assumes that the script will be attached to the spreadsheet on which it is to operate, which you're likely already doing.此脚本假定该脚本将附加到要对其进行操作的电子表格,您可能已经在这样做了。

function myFunction() {
   const ss = SpreadsheetApp.getActiveSpreadsheet();
   const mainSheet = ss.getSheetByName("MainSheet");
   const neededSheets = ["Cats", "Dogs"];  
  
   mainSheet.clearFormats().clear() // clear formatting and values on main sheet
   
   
  for(var i=0; i<neededSheets.length; i++) 
     {
      var sheet = ss.getSheetByName(neededSheets[i]) ;
      var values = sheet.getRange(1,1,sheet.getLastRow(),13).getValues(); 
      mainSheet.getRange(1,i*13+1, values.length, values[0].length).setValues(values);
       
      var sourceRange = sheet.getRange(1,1,sheet.getLastRow(),13) // define the range that has the formatting you want to copy
      var targetRange = mainSheet.getRange(1,i*13+1, values.length, values[0].length) // define the range that you want to copy the formatting to
      sourceRange.copyTo(targetRange, {formatOnly:true}) // copy the format
      
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何制作一张纸的副本并将其保存到Google表格中的文件夹中 - How to make a copy of a single sheet and save it to a folder in google sheets Google表格:根据单元格值将一行数据移动到另一张表格 - Google Sheets: Move a row of data to another sheet based on cell value 谷歌表格 - 根据特定列将行移动到另一张表格 - Google Sheets - Moving row to another sheet based on specific column Google表格将一行移动到当前行中2个单元格条件的另一张表格 - Google Sheets move a row to another sheet on 2 cell criteria in current row Google表格脚本:自动将一行删除到另一张表格中 - Google Sheets Script : Automatically remove a row into another sheet 谷歌表格 - 宏不会将图表的格式复制到另一个表格 - Google Sheets - Macro doesn´t copy the format of a Chart to another Sheet 通过for循环谷歌表格脚本将行复制到另一张表格 - Copy row to another sheet via for loop google sheets script 将工作表 1 与工作表 2 进行比较并输出至工作表 3。Google 工作表。 JavaScript - Compare sheet 1 to sheet 2 and output to sheet 3. Google sheets. JavaScript 谷歌工作表中特定工作表的时间戳 - Timestamp to specific sheet in google sheets Google 工作表脚本将数据集从一张工作表复制到另一张工作表的顶部(仅限值) - Google sheets script copy data sets from one sheet to another (values only) at the top of the sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM