我有一个电子表格,人们会填写该电子表格,然后单击“提交”按钮。该按钮将创建PDF并将数据通过电子邮件发送给人们。每次有人提交数据时,一部分数据需要在另一个电子表格中进行编译。
我有可以复制范围并将其粘贴到电子表格中的代码。
function CopyDataToNewFile() {
var sss = SpreadsheetApp.openById('abcd1234'); // sss = source spreadsheet
var ss = sss.getSheetByName('Timesheet'); // ss = source sheet
//Get full range of data
var SRange = ss.getRange("A10:L22");
//get A1 notation identifying the range
var A1Range = SRange.getA1Notation();
//get the data values in range
var SData = SRange.getValues();
var tss = SpreadsheetApp.openById('abcd1234'); // tss = target spreadsheet
var ts = tss.getSheetByName('Info'); // ts = target sheet
var TRange = ts.getRange("A1:L13");
var T1Range = TRange.getA1Notation();
//set the target range to the values of the source data
ts.getRange(T1Range).setValues(SData);
}
此代码有两个问题:
我已经在寻找解决方案达数小时之久,并且不精通Javascript来编写任何自定义内容。谢谢!
答案 0 :(得分:0)
在下面的脚本中,您将看到如何选择单个单元格或整行以及如何将其内容复制到另一张纸上。
还有一个示例,说明如何检查单元格是否具有值。
您可以找到一种解决方案here,该方法可以将内容复制到现有工作表中而不覆盖现有工作表。
function CopyDataToNewFile() {
var sourceSheet = SpreadsheetApp.openById('ID'); // Selects your Source Spreadsheet by its ID
var ssheet = sourceSheet.getSheetByName('Timesheet'); // Selects your Source Sheet by its Name
var J6value = ssheet.getRange(6, 10).getValue(); // Get value from J6 in Source Sheet
var A14value = ssheet.getRange(14, 1).getValue(); // get value from A14 in Source Sheet
var the11thRow = ssheet.getRange('A11:11').getValues(); // Select the entire 11th row in the Source Sheet
var the14thRow = ssheet.getRange('A14:14').getValues(); // Select the entire 14th row in the Source Sheet
var targetSheet = SpreadsheetApp.openById('ID'); // Selects your Target Spreadsheet by its ID
var tsheet = targetSheet.getSheetByName('Info'); // Selects your Target Sheet by its name
var targetFor14thRow = tsheet.getRange('A11:11'); // Selects where your 11th row will be copied to, in this case simply row 11
var targetFor11thRow = tsheet.getRange('A14:14'); // Selects where your 14th row will be copied to, in this case simply row 14
if (!ssheet.getRange(14, 1).isBlank()) { // Conditional: If A14 in Source Sheet isn't empty
targetFor14thRow.setValues(the14thRow); // Then copy the entire 14th row to its target
}
tsheet.getRange(1,2).setValue(J6value); // Copy the value of J6 in the source document to 2A of the Target Sheet
tsheet.getRange(3,4).setValue(A14value); // Copy the value of A12 in the source document to C4 of the Target Sheet
}
编辑:使用.setValues而不是.copyTo来复制整个行。
答案 1 :(得分:0)
经过更多研究之后,我收集了更多代码,现在可以正常工作了。
function CopyDataToNewFile(targetSheetName,targetSsId, sourceSheetName,sourceSsId) {
var ss = SpreadsheetApp.openById('abcd1234sydfhnsgb').getSheetByName('Timesheet');
var ssd = SpreadsheetApp.openById('abcd1234srymsnzd').getSheetByName('Info');
var therapist = ss.getRange('D6:K6').getValues();
var sourceData10 = ss.getRange('A10:10').getValues();
ssd.getRange(ssd.getLastRow()+1,1,therapist.length,therapist[0].length).setValues(therapist);
if (!ss.getRange(10, 1).isBlank()) {
ssd.getRange(ssd.getLastRow()+1,1,sourceData10.length,sourceData10[0].length).setValues(sourceData10);
}
}