Google Sheets – App Script (copyTo – 单元格内容和格式)

huangapple go评论65阅读模式
英文:

Google Sheets - App Script (copyTo - cell content & format)

问题

I have a tracker to count daily productivity.

This is appended to a weekly tracker, in which the sum of the two serves as a running total.

The weekly tracker is appended to a separate sheet to serve as historical data.

TRACKER

DATA

As you can see in the 'DATA' screenshot, the formula is being copied from K12:O12

  • see function copyTT / source.copyTo

I want to retain cell formatting, and copy only the cell data, not its formula.

Script;

function mergeTT() { 

var ss = SpreadsheetApp.getActiveSpreadsheet (); 

var source = ss.getRange("NEW_TTRACKER!J3:O3"); 

var destSheet = ss.getSheetByName("NEW_TTRACKER"); 

var lastRow = destSheet.getRange("J7:J11").getValues().filter(String).length;

var destRange = destSheet.getRange(lastRow+7,10,1,6); 

source.copyTo(destRange, {contentsOnly: true});

var ws = ss.getSheetByName("NEW_TTRACKER")

ws.getRange('K3:O3').clearContent();

} 

function copyTT() { 

var ss = SpreadsheetApp.getActiveSpreadsheet (); 

var source = ss.getRange("NEW_TTRACKER!J5:O12"); 

var destSheet = ss.getSheetByName("NEW_TDATA");

var destRange = destSheet.getRange(destSheet.getLastRow()+1,1); 

source.copyTo(destRange, {contentsOnly: false});

var sheet = SpreadsheetApp.getActive().getSheetByName("NEW_TTRACKER"); 

sheet.getRange('J7:O11').clearContent(); 

} 

Thanks in advance.

I've been attempting to get around it playing with different CopyPasteType properties and I'm really not getting anywhere.

英文:

I have a tracker to count daily productivity.

This is appended to a weekly tracker, in which the sum of the two serves as a running total.

The weekly tracker is appended to a separate sheet to serve as historical data.

TRACKER

DATA

As you can see in the 'DATA' screenshot, the formula is being copied from K12:O12

  • see function copyTT / source.copyTo

I want to retain cell formatting, and copy only the cell data, not it's formula.

Script;

function mergeTT() { 

var ss = SpreadsheetApp.getActiveSpreadsheet (); 

var source = ss.getRange("NEW_TTRACKER!J3:O3"); 

var destSheet = ss.getSheetByName("NEW_TTRACKER"); 

var lastRow = destSheet.getRange("J7:J11").getValues().filter(String).length;

var destRange = destSheet.getRange(lastRow+7,10,1,6); 

source.copyTo(destRange, {contentsOnly: true});

var ws = ss.getSheetByName("NEW_TTRACKER")

ws.getRange('K3:O3').clearContent();

} 

function copyTT() { 

var ss = SpreadsheetApp.getActiveSpreadsheet (); 

var source = ss.getRange("NEW_TTRACKER!J5:O12"); 

var destSheet = ss.getSheetByName("NEW_TDATA");

var destRange = destSheet.getRange(destSheet.getLastRow()+1,1); 

source.copyTo(destRange, {contentsOnly: false});

var sheet = SpreadsheetApp.getActive().getSheetByName("NEW_TTRACKER"); 

sheet.getRange('J7:O11').clearContent(); 

} 

Thanks in advance.

I've been attempting to get around it playing with different CopyPasteType properties and I'm really not getting anywhere.

答案1

得分: 0

关于“我一直在尝试解决它,尝试不同的CopyPasteType属性”,我认为你的方法是正确的。在你的脚本中,可以尝试按照以下方式修改你的脚本:

从:

source.copyTo(destRange, {contentsOnly: true});

到:

source.copyTo(destRange, { contentsOnly: true });
source.copyTo(destRange, SpreadsheetApp.CopyPasteType.PASTE_FORMAT, false); // 添加的部分
  • 通过这种修改,格式也会被复制。

参考链接:

英文:

About I've been attempting to get around it playing with different CopyPasteType properties, I think that your approach is correct. In your script, how about modifying your script as follows?

From:

source.copyTo(destRange, {contentsOnly: true});

To:

source.copyTo(destRange, { contentsOnly: true });
source.copyTo(destRange, SpreadsheetApp.CopyPasteType.PASTE_FORMAT, false); // Added
  • By this modification, the format is also copied.

Reference:

huangapple
  • 本文由 发表于 2023年2月10日 09:59:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75406254.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定