希望这个问题尚未得到解答。我花了相当多的时间进行搜索,虽然我找到了类似的帖子,但没有一个完全符合我的目的。
我想使用Google Apps脚本将单个工作表从Google电子表格复制到另一个Google电子表格中,我希望保留格式(包括合并的单元格)。有没有办法做到这一点?我尝试了以下功能:
CopyTo从() copyFormatToRange()
但这些方法仅适用于同一电子表格,不允许在不同的电子表格之间复制数据。有没有人有什么建议?谢谢!
答案 0 :(得分:17)
你看过这里:
https://developers.google.com/apps-script/reference/spreadsheet/sheet#copyTo(Spreadsheet)
CopyTo从(电子表格)
将工作表复制到另一个电子表格。目标电子表格可以是来源。新电子表格的名称为“原始电子表格名称的副本”。
var source = SpreadsheetApp.getActiveSpreadsheet();
var sheet = source.getSheets()[0];
var destination = SpreadsheetApp.openById("ID_GOES HERE");
sheet.copyTo(destination);
答案 1 :(得分:2)
如果要复制特定电子表格的表格,可以使用:
SpreadsheetApp.getActiveSpreadsheet().duplicateActiveSheet();
这将创建当前工作表的副本,并使该副本处于活动状态。
答案 2 :(得分:1)
万一任何人只想在特定的工作表中复制特定范围的格式
/**
copying full formatting including sizez and merging from one range to new location
https://stackoverflow.com/questions/25106580/copy-value-and-format-from-a-sheet-to-a-new-google-spreadsheet-document
below first coordinates of original range we want to copy and then cooridinaes of the begining of the place ino which we want to copy our range
@param startColumnOfOriginal {Number}
@param startRowOfOriginal {Number}
@param numberOfRows {Number}
@param numberOfColumns {Number}
@param startColumnOfTarget {Number}
@param startRowOfTarget {Number}
@param sheetOfOrigin {Sheet} sheet object of where our source is
@param sheetOfTarget {Sheet} sheet object where we want to copy it
*/
function copyFullFormatting(startRowOfOriginal,startColumnOfOriginal,numberOfRows
,numberOfColumns, startRowOfTarget, startColumnOfTarget, sheetOfOrigin, sheetOfTarget
){
const sourceRange = sheetOfOrigin.getRange(
startRowOfOriginal, startColumnOfOriginal, numberOfRows, numberOfColumns)
const targetRange = sheetOfTarget.getRange(
startRowOfTarget, startColumnOfTarget, numberOfRows, numberOfColumns)
sourceRange.copyFormatToRange(sheetOfTarget,startColumnOfOriginal, startColumnOfTarget+ numberOfColumns, startRowOfTarget, startRowOfTarget+numberOfRows )
//iterating over rows of source range
for(var rowNumb=startRowOfOriginal;rowNumb<startRowOfOriginal+numberOfRows;rowNumb++ ){
const targetRowNumb = rowNumb-startRowOfOriginal+startRowOfTarget
sheetOfTarget.setRowHeight(targetRowNumb, sheetOfOrigin.getRowHeight(rowNumb))
}
// iterating over columns in target range
for (var colNumb=startColumnOfOriginal;colNumb<startColumnOfOriginal+numberOfColumns;colNumb++ ){
const targetColNumb = colNumb-startColumnOfOriginal+startColumnOfTarget
sheetOfTarget.setColumnWidth(targetColNumb, sheetOfOrigin.getColumnWidth(colNumb))
}
}