简体   繁体   English

Google App脚本-导入2个SpreadSheets之间的所有范围格式

[英]Google App Script - Import ALL range formatting between 2 SpreadSheets

Update: I wrote for loops to fix the merged cell and column width issues. 更新:我写了for循环来解决合并的单元格和列宽问题。 Just need some help with the borders! 只需一些边界帮助!

I'm working on copying a range of cells from one Google Spreadsheet to another Google Spreadsheet. 我正在将一系列单元格从一个Google Spreadsheet复制到另一个Google Spreadsheet。 The difficult part is that I'm trying to keep ALL formatting. 困难的部分是我试图保持所有格式。 I have been able to preserve all of the formatting thus far, except for 3 items: cell borders, various merged cells, and column width. 到目前为止,我已经能够保留所有格式,除了3项内容:单元格边框,各种合并的单元格和列宽。 Any help would be greatly appreciated. 任何帮助将不胜感激。 Sorry in advance, just found out about Google App Script yesterday and I've never used JS before. 抱歉,昨天才发现有关Google App Script的信息,而我以前从未使用过JS。

function importTable() {

  // Source spreadsheet
  var srcSpreadSheet = SpreadsheetApp.openById("SpreadSheet-ID");
  var scrSheet = srcSpreadSheet.setActiveSheet(srcSpreadSheet.getSheetByName("Sheet1"));

  // Destination spreadsheet
  var destSpreadSheet = SpreadsheetApp.openById("SpreadSheet-ID");
  var destSheet = destSpreadSheet.setActiveSheet(destSpreadSheet.getSheetByName("Test"));
  destSheet.clear();

  // Get data and formatting from the source sheet
  var range = scrSheet.getRange(1, 2, 24, 16);

  var values = range.getValues();
  var background = range.getBackgrounds();
  var banding = range.getBandings();
  var mergedRanges = range.getMergedRanges();
  var fontColor = range.getFontColors();
  var fontFamily = range.getFontFamilies();
  var fontLine = range.getFontLines();
  var fontSize = range.getFontSizes();
  var fontStyle = range.getFontStyles();
  var fontWeight = range.getFontWeights();
  var horAlign = range.getHorizontalAlignments();
  var textStyle = range.getTextStyles();
  var vertAlign = range.getVerticalAlignments();

  // Put data and formatting in the destination sheet
  var destRange = destSheet.getRange(1, 2, 24, 16);

  destRange.setValues(values);
  destRange.setBackgrounds(background);
  destRange.setFontColors(fontColor);
  destRange.setFontFamilies(fontFamily);
  destRange.setFontLines(fontLine);
  destRange.setFontSizes(fontSize);
  destRange.setFontStyles(fontStyle);
  destRange.setFontWeights(fontWeight);
  destRange.setHorizontalAlignments(horAlign);
  destRange.setTextStyles(textStyle);
  destRange.setVerticalAlignments(vertAlign);

    // Iterate through to put merged ranges in place
  for (var i = 0; i < mergedRanges.length; i++) {
    destSheet.getRange(mergedRanges[i].getA1Notation()).merge();
  }

  // Iterate through to get the column width of the source destination
  for (var i = 1; i < 18; i++) {
    var width = scrSheet.getColumnWidth(i);
    destSheet.setColumnWidth(i, width);
  }

  // Iterate through to get the row heighth of the source destination
  for (var i = 1; i < 27; i++){
    var height = scrSheet.getRowHeight(i);
    destSheet.setRowHeight(i, height);
  }

}

How about this workaround? 这个解决方法如何? In this workaround, copyTo() of Class Sheet and copyTo() of Class Range are used. 在此变通办法中,使用“类表”的copyTo()和“类范围”的copyTo()。 I think that there are several answers for your situation. 我认为您的情况有几个答案。 So please think of this as one of them. 因此,请将此视为其中之一。

The flow of this script is as follows. 该脚本的流程如下。

  1. Copy a sheet including the range you want to copy as a template using copyTo() of Class Sheet. 使用类表的copyTo()将包含要复制的范围的表复制为模板。
  2. Copy values, formulas, formats and merges from the copied sheet to the destination sheet using copyTo() of Class Range. 使用类范围的copyTo()复制值,公式,格式并将其从复制的工作表合并到目标工作表。
    • At that time, the range you want to copy is used. 那时,将使用您要复制的范围。
  3. Copy column width from from the copied sheet to the destination sheet using copyTo() of Class Range. 使用Class Range的copyTo()将列宽从复制的工作表复制到目标工作表。
  4. Delete the template sheet. 删除模板表。

Sample script : 示例脚本:

function importTable2() {
  // Source
  var sourceSheet = SpreadsheetApp.openById("SpreadSheet-ID").getSheetByName("Sheet1");
  var sourceRange = sourceSheet.getRange(1, 2, 24, 16);

  // Destination
  var destSS = SpreadsheetApp.openById("SpreadSheet-ID");
  var destSheet = destSS.getSheetByName("Test");
  var destRange = destSheet.getRange(1, 2, 24, 16);
  destSheet.clear();

  // Copy
  var copiedsheet = sourceRange.getSheet().copyTo(destSS);
  copiedsheet.getRange(sourceRange.getA1Notation()).copyTo(destRange);
  copiedsheet.getRange(sourceRange.getA1Notation()).copyTo(destRange, SpreadsheetApp.CopyPasteType.PASTE_COLUMN_WIDTHS, false);
  destSS.deleteSheet(copiedsheet);
}

Note : 注意 :

  • Before you run this script, please set SpreadSheet-ID . 在运行此脚本之前,请设置SpreadSheet-ID

References : 参考文献:

If I misunderstand your question, please tell me. 如果我误解了您的问题,请告诉我。 I would like to modify it. 我想修改它。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM