我想创建一个脚本来将工作表的活动行(TIME MACHINE)复制到另一个Google电子表格的工作表(数据库)。我还想将“1”发送到复制行的13行。如果有人能指导我会很棒。下面给出的代码仅适用于在同一电子表格中复制。
function endTime() {
transfer("1E3CWWohirN4DD_f_23WWBElYrhqnAs_27crrxowFjFA", "TIME MACHINE", "TIME MACHINE2")
}
function transfer(targetId, sourceSheetName, targetSheetName) {
var d = new Date();
var offset = -d.getTimezoneOffset()/60;
var h = d.getHours();
var h = h >= 13? h - 12: h;
h = h == 0? 12: h;
var m = m < 10? "0" + d.getMinutes(): d.getMinutes();
var s = d.getSeconds();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName(sourceSheetName);
var last = sourceSheet.getActiveRange().getRow();
var data = sourceSheet.getRange(last, 1, 1, 14).getValues();
var time1 = h + ":" + m + ":" + s;
sourceSheet.getRange(last, 28).setValue(time1);
var tamma = sourceSheet.getRange(last, 11).getValue();
// copy data
var ss2 = SpreadsheetApp.openById(targetId);
var targetSheet = ss2.getSheetByName(targetSheetName);
//get last row
var lastRow = targetSheet.getLastRow();
//write data
targetSheet.getRange(lastRow + 1, 1, data.length, data[0].length).setValues(data);
//write "1" into column 13
targetSheet.getRange(lastRow + 1, 13, data.length, 1).setValue("1");
targetSheet.getRange(lastRow + 1, 11, data.length, 1).setValue(tamma);
}
答案 0 :(得分:0)
您的代码只需要进行一些重组,您需要从源表中获取正确的数据。
这应该有效:
function main() {
transfer("1mEluY-_K9oBvbxzASKn25YZ8BgF_naLl0XNitb4zIA8", "Sheet1", "Sheet1");
}
function transfer(targetId, sourceSheetName, targetSheetName) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName(sourceSheetName);
var last = sourceSheet.getActiveRange().getRow();
var data = sourceSheet.getRange(last, 1, 1, 14).getValues();
// copy data
var ss2 = SpreadsheetApp.openById(targetId);
var targetSheet = ss2.getSheetByName(targetSheetName);
//get last row
var lastRow = targetSheet.getLastRow();
//write data
targetSheet.getRange(lastRow + 1, 1, data.length, data[0].length)
.setValues(data);
//write "1" into column 13
targetSheet.getRange(lastRow + 1, 13, data.length, 1)
.setValue("1")
}