Google apps script GAS使用货币和百分比格式将数据复制到另一个电子表格

Google apps script GAS使用货币和百分比格式将数据复制到另一个电子表格,google-apps-script,google-sheets,Google Apps Script,Google Sheets,我有一段代码,它使用moveTo()函数将数据从一个电子表格复制到另一个电子表格。数据由$和%组成,但该格式不会复制到第二张工作表 第二张纸上是否有“硬定”格式?还是有更好的方法让我这么做 注**无论出于何种原因,这段代码在旧工作表上运行良好,但在我移动到新工作表时停止 //Get full range of data var SRange = ss.getRange('B3:B9'); var EndInv = ss.getRange('B11:B15'); var Theoret

我有一段代码,它使用moveTo()函数将数据从一个电子表格复制到另一个电子表格。数据由$和%组成,但该格式不会复制到第二张工作表

第二张纸上是否有“硬定”格式?还是有更好的方法让我这么做

注**无论出于何种原因,这段代码在旧工作表上运行良好,但在我移动到新工作表时停止

//Get full range of data
  var SRange = ss.getRange('B3:B9');
  var EndInv = ss.getRange('B11:B15');
  var Theoretical = ss.getRange('B17:B21');
  var Purchases = costReportPurchases.getRange('D12:D16');

//get A1 notation identifying the range
  var A1Range = SRange.getA1Notation();
  var EndInvRange = EndInv.getA1Notation();
  var TheoreticalRange = Theoretical.getA1Notation();
  var PurchasesRange = Purchases.getA1Notation();

//get the data values in range
  var SData = SRange.getValues();
  var EndInvData = EndInv.getValues();
  var TheoreticalData = Theoretical.getValues();
  var PurchasesData = Purchases.getValues();

//Makes the move to the second sheet
  var tss = SpreadsheetApp.openById('xxxxxxxxxx'); // tss = target spreadsheet - Snap Shot
  var ts = tss.getSheetByName('CompanySnapShot'); // ts = target sheet
  var cedarvilleCOGS = ts.getRange('F55:F61'); //Sets range on Snap Shot
  var cedarvilleEndInv = ts.getRange('F64:F68'); //Sets range on Snap Shot
  var cedarvilleTheoretical = ts.getRange('F93:F97'); //Sets range on Snap Shot
  var cedarvillePurchases = ts.getRange('F107:F111'); //Sets range on Snap Shot
  ts.getRange(A1Range).setValues(SData).moveTo(cedarvilleCOGS);
  ts.getRange(EndInvRange).setValues(EndInvData).moveTo(cedarvilleEndInv);
  ts.getRange(TheoreticalRange).setValues(TheoreticalData).moveTo(cedarvilleTheoretical);
  ts.getRange(PurchasesRange).setValues(PurchasesData).moveTo(cedarvillePurchases);

当我遇到同样的问题时,我在写入值后使用了
copyFormatorRange

读一读这个。

举个例子

 ts.getRange(A1Range).setValues(SData).moveTo(cedarvilleCOGS);
 srange.copyFormatToRange(CompanySnapShot,6,6,55,61);
这应该(我希望)从源范围复制格式化,并将其应用到目标

我还没有测试上面的代码


您还可以查看使用NamedRanges,然后在写入数据后设置格式。

因此,我通过调用方法
setNumberFormats()


我觉得这样应该行得通,但是不行。啊。复制数据后我该如何设置格式?这当然会完成添加
srange.copyFormatorRange(CompanySnapShot,6,6,55,61);
ts.getRange(A1Range).setValues(SData).moveTo(cedarvilleCOGS)后的操作
是的,但未定义srange。我尝试过使用它,但无法使其发挥作用。我从文档中获得了示例,但仅在同一个电子表格中。当我尝试通过工作表id将其发送到另一个时,会出现错误“无法将范围转换为(类)”抱歉,它应该是
SRange
SR大写,也可能是您的源范围是一列,目标范围是一行。您是否能够共享工作表的示例?我们的域已被锁定,无法共享。在我更改为SR后,它现在挂在“CompanySnapShot”SRange.copyFormatorRange(CompanySnapShot,6,55,61)上;错误“无法将电子表格转换为(类)。”
//This is for % based numbers
var formatFoodPercentPWSouth = [["00.00%","00.00%","00.00%","00.00%","00.00%","00.00%","00.00%",]];
  var rangeFoodPercentPWSouth = CompanySnapShot.getRange("B75:H75");
  rangeFoodPercentPWSouth.setNumberFormats(formatFoodPercentPWSouth);

//This is for $ based numbers
var formatLiquorDollarsEndingInventorySouth = [["$00,000.00","$00,000.00","$00,000.00","$00,000.00","$00,000.00","$00,000.00","$00,000.00",]];
 var rangeLiquorDollarEndingInventorySouth = CompanySnapShot.getRange("B79:H79");
 rangeLiquorDollarEndingInventorySouth.setNumberFormats(formatLiquorDollarsEndingInventorySouth);