Google Apps 脚本使用格式/公式将范围从一个电子表格复制到另一个电子表格
Google Apps Script Copy Range From One SpreadSheet To Another With Formats / Formulas
此代码运行良好,但我很难确定如何将格式和公式与数据一起复制。请给我任何帮助。
var MyWBA = SpreadsheetApp.openById('1234567');
var MySHA = MyWBA.getSheetByName('Expense');
var SRange = MySHA.getDataRange();
var A1Range = SRange.getA1Notation();
var SData = SRange.getValues();
var MyWBB =
SpreadsheetApp.openById('122454');
var MySHB = MyWBB.getSheetByName('Income');
MySHB.getRange(A1Range).clearContent()
MySHB.getRange(A1Range).setValues(SData);
这个样本怎么样?此示例从源电子表格中复制每个单元格的值、公式和格式,并将它们粘贴到目标电子表格。
var srcID = "### Source Spreadsheet ID ###";
var src_sheetName = "### Source sheet name ###";
var dstID = "### Destination Spreadsheet ID ###";
var dst_sheetName = "### Destination sheet name ###";
var src_ss = SpreadsheetApp.openById(srcID);
var src = src_ss.getSheetByName(src_sheetName);
var srcrange = src.getDataRange();
var srcrangeA1 = srcrange.getA1Notation();
var src_values = srcrange.getValues();
var src_formulas = srcrange.getFormulas();
var src_formats = srcrange.getNumberFormats();
for (var i=0; i<src_formulas.length; i++) {
for (var j=0; j<src_formulas[i].length; j++) {
if (src_formulas[i][j]) {
src_values[i][j] = src_formulas[i][j];
}
}
}
var dst_ss = SpreadsheetApp.openById(dstID);
var dst = dst_ss.getSheetByName(dst_sheetName);
dst.getRange(srcrangeA1).setValues(src_values);
dst.getRange(srcrangeA1).setNumberFormats(src_formats);
此代码运行良好,但我很难确定如何将格式和公式与数据一起复制。请给我任何帮助。
var MyWBA = SpreadsheetApp.openById('1234567');
var MySHA = MyWBA.getSheetByName('Expense');
var SRange = MySHA.getDataRange();
var A1Range = SRange.getA1Notation();
var SData = SRange.getValues();
var MyWBB =
SpreadsheetApp.openById('122454');
var MySHB = MyWBB.getSheetByName('Income');
MySHB.getRange(A1Range).clearContent()
MySHB.getRange(A1Range).setValues(SData);
这个样本怎么样?此示例从源电子表格中复制每个单元格的值、公式和格式,并将它们粘贴到目标电子表格。
var srcID = "### Source Spreadsheet ID ###";
var src_sheetName = "### Source sheet name ###";
var dstID = "### Destination Spreadsheet ID ###";
var dst_sheetName = "### Destination sheet name ###";
var src_ss = SpreadsheetApp.openById(srcID);
var src = src_ss.getSheetByName(src_sheetName);
var srcrange = src.getDataRange();
var srcrangeA1 = srcrange.getA1Notation();
var src_values = srcrange.getValues();
var src_formulas = srcrange.getFormulas();
var src_formats = srcrange.getNumberFormats();
for (var i=0; i<src_formulas.length; i++) {
for (var j=0; j<src_formulas[i].length; j++) {
if (src_formulas[i][j]) {
src_values[i][j] = src_formulas[i][j];
}
}
}
var dst_ss = SpreadsheetApp.openById(dstID);
var dst = dst_ss.getSheetByName(dst_sheetName);
dst.getRange(srcrangeA1).setValues(src_values);
dst.getRange(srcrangeA1).setNumberFormats(src_formats);