Google apps script 谷歌工作表的谷歌脚本-范围复制粘贴特殊“添加”

Google apps script 谷歌工作表的谷歌脚本-范围复制粘贴特殊“添加”,google-apps-script,google-sheets,Google Apps Script,Google Sheets,我真的是一个脚本新手,我以前使用微软office做我的工作,它有一个特殊的粘贴添加功能,而现在对于google sheet我真的找不到它 我将有一个源范围C2:C102和目标在同一个表D2:D102我希望脚本,我可以手动运行每周从源复制所有的范围,并与现有的数据在D2:D102值总和 这里有一个小例子- 我试图使用这段代码,但它只是替换了这些值 function copyCells(){ var thisSpreadsheet = SpreadsheetApp.getActiveSprea

我真的是一个脚本新手,我以前使用微软office做我的工作,它有一个特殊的粘贴添加功能,而现在对于google sheet我真的找不到它

我将有一个源范围C2:C102和目标在同一个表D2:D102我希望脚本,我可以手动运行每周从源复制所有的范围,并与现有的数据在D2:D102值总和

这里有一个小例子-

我试图使用这段代码,但它只是替换了这些值

function copyCells(){
   var thisSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var SourceSheet = thisSpreadsheet.getSheetByName("test");
  var SourceRange = thisSpreadsheet.getRange("C2:C102");

  var destinationSheet = thisSpreadsheet.getSheetByName("test");
  var destinationRange = destinationSheet.getRange("D2:D102");

   SourceRange.copyTo(destinationRange, {contentsOnly: true}); 
}

任何帮助都将不胜感激:

尚未测试代码,请尝试此操作

使用getValues获取值 求和 使用setValues复制回值

参考资料


看起来它在工作,我唯一的问题是当我添加十进制数时,例如,如果C2有12,5的值,它将只添加12
function copyCells(){
   const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
   const SourceSheet = spreadsheet.getSheetByName("test");
   const SourceRange = spreadsheet.getRange("C2:C102");
   const SourceValues = SourceRange.getValues();

   const destinationSheet = spreadsheet.getSheetByName("test");
   const destinationRange = destinationSheet.getRange("D2:D102");
   const destinationValues = destinationRange.getValues();

   for (let i = 0; i < SourceValues.length; i++)
     destinationValues[i][0] = parseFloat(destinationValues[i][0]) + parseFloat(SourceValues[i][0])

   destinationRange.setValues(destinationValues);
}