我有一个脚本,在运行时将单元格添加在一起(即A1 + A2并在A1中返回结果),有2个表,每个都是2 x 17,所以目前我有一个代码块,每个单元格都有一个通过数组做到这一点的方法?这是我目前使用的脚本。
function Transfer() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var num1 = ss.getRange("Monday-Saturday!C5").getValue();
var num2 = ss.getRange("2ndTrailer!B3").getValue();
ss.getRange("Monday-Saturday!C5").setValue(num1+num2);
var num1 = ss.getRange("Monday-Saturday!D5").getValue();
var num2 = ss.getRange("2ndTrailer!C3").getValue();
ss.getRange("Monday-Saturday!D5").setValue(num1+num2);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var num1 = ss.getRange("Monday-Saturday!C6").getValue();
var num2 = ss.getRange("2ndTrailer!B4").getValue();
ss.getRange("Monday-Saturday!C6").setValue(num1+num2);
var num1 = ss.getRange("Monday-Saturday!D6").getValue();
var num2 = ss.getRange("2ndTrailer!C4").getValue();
ss.getRange("Monday-Saturday!D6").setValue(num1+num2);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var num1 = ss.getRange("Monday-Saturday!C7").getValue();
var num2 = ss.getRange("2ndTrailer!B5").getValue();
ss.getRange("Monday-Saturday!C7").setValue(num1+num2);
var num1 = ss.getRange("Monday-Saturday!D7").getValue();
var num2 = ss.getRange("2ndTrailer!C5").getValue();
ss.getRange("Monday-Saturday!D7").setValue(num1+num2);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var num1 = ss.getRange("Monday-Saturday!C8").getValue();
var num2 = ss.getRange("2ndTrailer!B6").getValue();
ss.getRange("Monday-Saturday!C8").setValue(num1+num2);
var num1 = ss.getRange("Monday-Saturday!D8").getValue();
var num2 = ss.getRange("2ndTrailer!C6").getValue();
ss.getRange("Monday-Saturday!D8").setValue(num1+num2);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var num1 = ss.getRange("Monday-Saturday!C9").getValue();
var num2 = ss.getRange("2ndTrailer!B7").getValue();
ss.getRange("Monday-Saturday!C9").setValue(num1+num2);
var num1 = ss.getRange("Monday-Saturday!D9").getValue();
var num2 = ss.getRange("2ndTrailer!C7").getValue();
ss.getRange("Monday-Saturday!D9").setValue(num1+num2);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var num1 = ss.getRange("Monday-Saturday!C11").getValue();
var num2 = ss.getRange("2ndTrailer!B9").getValue();
ss.getRange("Monday-Saturday!C11").setValue(num1+num2);
var num1 = ss.getRange("Monday-Saturday!D11").getValue();
var num2 = ss.getRange("2ndTrailer!C9").getValue();
ss.getRange("Monday-Saturday!D11").setValue(num1+num2);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var num1 = ss.getRange("Monday-Saturday!C12").getValue();
var num2 = ss.getRange("2ndTrailer!B10").getValue();
ss.getRange("Monday-Saturday!C12").setValue(num1+num2);
var num1 = ss.getRange("Monday-Saturday!D12").getValue();
var num2 = ss.getRange("2ndTrailer!C10").getValue();
ss.getRange("Monday-Saturday!D12").setValue(num1+num2);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var num1 = ss.getRange("Monday-Saturday!C13").getValue();
var num2 = ss.getRange("2ndTrailer!B11").getValue();
ss.getRange("Monday-Saturday!C13").setValue(num1+num2);
var num1 = ss.getRange("Monday-Saturday!D13").getValue();
var num2 = ss.getRange("2ndTrailer!C11").getValue();
ss.getRange("Monday-Saturday!D13").setValue(num1+num2);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var num1 = ss.getRange("Monday-Saturday!C14").getValue();
var num2 = ss.getRange("2ndTrailer!B12").getValue();
ss.getRange("Monday-Saturday!C14").setValue(num1+num2);
var num1 = ss.getRange("Monday-Saturday!D14").getValue();
var num2 = ss.getRange("2ndTrailer!C12").getValue();
ss.getRange("Monday-Saturday!D14").setValue(num1+num2);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var num1 = ss.getRange("Monday-Saturday!C15").getValue();
var num2 = ss.getRange("2ndTrailer!B13").getValue();
ss.getRange("Monday-Saturday!C15").setValue(num1+num2);
var num1 = ss.getRange("Monday-Saturday!D15").getValue();
var num2 = ss.getRange("2ndTrailer!C13").getValue();
ss.getRange("Monday-Saturday!D15").setValue(num1+num2);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var num1 = ss.getRange("Monday-Saturday!C16").getValue();
var num2 = ss.getRange("2ndTrailer!B14").getValue();
ss.getRange("Monday-Saturday!C16").setValue(num1+num2);
var num1 = ss.getRange("Monday-Saturday!D16").getValue();
var num2 = ss.getRange("2ndTrailer!C14").getValue();
ss.getRange("Monday-Saturday!D16").setValue(num1+num2);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var num1 = ss.getRange("Monday-Saturday!C17").getValue();
var num2 = ss.getRange("2ndTrailer!B15").getValue();
ss.getRange("Monday-Saturday!C17").setValue(num1+num2);
var num1 = ss.getRange("Monday-Saturday!D17").getValue();
var num2 = ss.getRange("2ndTrailer!C15").getValue();
ss.getRange("Monday-Saturday!D17").setValue(num1+num2);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var num1 = ss.getRange("Monday-Saturday!C18").getValue();
var num2 = ss.getRange("2ndTrailer!B16").getValue();
ss.getRange("Monday-Saturday!C18").setValue(num1+num2);
var num1 = ss.getRange("Monday-Saturday!D18").getValue();
var num2 = ss.getRange("2ndTrailer!C16").getValue();
ss.getRange("Monday-Saturday!D18").setValue(num1+num2);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var num1 = ss.getRange("Monday-Saturday!C19").getValue();
var num2 = ss.getRange("2ndTrailer!B17").getValue();
ss.getRange("Monday-Saturday!C19").setValue(num1+num2);
var num1 = ss.getRange("Monday-Saturday!D19").getValue();
var num2 = ss.getRange("2ndTrailer!C17").getValue();
ss.getRange("Monday-Saturday!D19").setValue(num1+num2);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var num1 = ss.getRange("Monday-Saturday!C20").getValue();
var num2 = ss.getRange("2ndTrailer!B18").getValue();
ss.getRange("Monday-Saturday!C20").setValue(num1+num2);
var num1 = ss.getRange("Monday-Saturday!D20").getValue();
var num2 = ss.getRange("2ndTrailer!C18").getValue();
ss.getRange("Monday-Saturday!D20").setValue(num1+num2);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var num1 = ss.getRange("Monday-Saturday!C21").getValue();
var num2 = ss.getRange("2ndTrailer!B19").getValue();
ss.getRange("Monday-Saturday!C21").setValue(num1+num2);
var num1 = ss.getRange("Monday-Saturday!D21").getValue();
var num2 = ss.getRange("2ndTrailer!C19").getValue();
ss.getRange("Monday-Saturday!D21").setValue(num1+num2);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var num1 = ss.getRange("Monday-Saturday!C22").getValue();
var num2 = ss.getRange("2ndTrailer!B20").getValue();
ss.getRange("Monday-Saturday!C22").setValue(num1+num2);
var num1 = ss.getRange("Monday-Saturday!D22").getValue();
var num2 = ss.getRange("2ndTrailer!C20").getValue();
ss.getRange("Monday-Saturday!D22").setValue(num1+num2);
var sheet = SpreadsheetApp.getActive().getSheetByName('2ndTrailer');
sheet.getRange('A2').clearContent();
var sheet = SpreadsheetApp.getActive().getSheetByName('Monday-Saturday');
sheet.getRange('A23').setValue('YES');
}
由于
答案 0 :(得分:0)
以下是包含数组和循环的代码:
function Transfer() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s1 = ss.getSheetByName('Monday-Saturday');
var s2 = ss.getSheetByName('2ndTrailer');
var data1 = s1.getRange(5, 3, 18, 2).getValues();
var data2 = s2.getRange(3, 2, 18, 2).getValues();
var newData = [];
for(var i = 0 ; i < data1.length; i++){
var newValueC = data1[i][0] + data2[i][1];
var newValueD = data1[i][1] + data2[i][1];
newData.push([newValueC, newValueD]);
}
s1.getRange(5, 3, 18, 2).setValues(newData);
s2.getRange('A2').clearContent();
s1.getRange('A23').setValue('YES');
}
有点难看,但应该这样做。
编辑:做了一些更改,因为range.setValue()方法导致了问题