简体   繁体   English

在工作表之间传输特定的单元格数据

[英]Transferring specific cell data from sheet to sheet

I have a problem where I need to transfer the cell data from one sheet to another. 我有一个问题,我需要将单元格数据从一张纸传输到另一张纸。 What I'm trying to achieve in the end is that if I click a button, it will import my data from the sheet one sheet to the sheet I'm currently using. 最后,我要实现的目标是,如果我单击一个按钮,它将把我的数据从一张工作表导入到我当前正在使用的工作表中。

My original code looked like this: 我的原始代码如下所示:

function C2() {
  var sourceSheet  = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("C2");

var destSheet  = SpreadsheetApp.getActiveSpreadsheet()
  sourceSheet.getRange("A1:I43").copyTo(destSheet.getRange("A1:I43"), {contentsOnly:true});
}

But that won't work for the new modifications I've made. 但这不适用于我所做的新修改。 It's erasing the formulas in the cells on the next sheet and replacing them with the values of the previous sheet that the formulas produced. 它会擦除下一张工作表中单元格中的公式,并将其替换为公式所产生的上一张工作表中的值。

I've tried to use this formula, selecting all of the areas that I'd like to transfer, but it results in a "range not found" error message. 我尝试使用此公式,选择了我要转移的所有区域,但是会导致出现“找不到范围”错误消​​息。

function C1() {
  var sourceSheet  = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("C1");

var destSheet  = SpreadsheetApp.getActiveSpreadsheet()
  sourceSheet.getRange("A2,A4:B16,A21,A23,A25,A27,D3:D22,E4:E22,F3:H22,C24,C26:E31,F25,L1").copyTo(destSheet.getRange("A2,A4:B16,A21,A23,A25,A27,D3:D22,E4:E22,F3:H22,C24,C26:E31,F25,L1"), {contentsOnly:true});
}

If it's possible to cherry pick the cells I need and transfer them to my desired sheet instead of only using a broad range (ex. "A1:I43"), That would be ideal. 如果可以挑选需要的单元格并将其转移到所需的工作表上,而不是仅使用较大范围的单元格(例如“ A1:I43”),那将是理想的选择。 Any and all help is very much appreciated. 任何帮助都将不胜感激。

You need to define a RangeList and iterate through all the contained ranges. 您需要定义一个RangeList并遍历所有包含的范围。

If you want to copy values only, getValues() and setValues() will be useful methods for you: 如果只想复制值,则getValues()setValues()将是对您有用的方法:

function myFunction() {
  var spreadsheet=SpreadsheetApp.getActiveSpreadsheet();
  var sourceSheet  = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("C2");
  var destSheet  = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  var rangeList=sourceSheet.getRangeList(["A2","A4:B16","A21","A23","A25","A27","D3:D22","E4:E22","F3:H22","C24","C26:E31","F25","L1"]);
  var rangeList2=destSheet.getRangeList(["A2","A4:B16","A21","A23","A25","A27","D3:D22","E4:E22","F3:H22","C24","C26:E31","F25","L1"]); 
  for (var i=0;i<rangeList.getRanges().length;i++){
       rangeList2.getRanges()[i].setValues(rangeList.getRanges()[i].getValues());
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM