我有一个功能,我从网上获得并稍微修改以供我自己使用。我无法工作的是如何删除整行,只复制下面函数中的特定行。
我希望能够将变量传递给每个工作表的函数及其相应的列进行复制。
离。 moveCompletedRows(type,whereToMoveType,[cell to the move to move]){}
function moveCompletedRows(type,whereToMoveType) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('SortBot');
var targetSheet = ss.getSheetByName(whereToMoveType);
var val = sheet.getDataRange().getValues();
var headers = val.shift();
var arr = [], rowsToWriteBack = [];
rowsToWriteBack.push(headers);
val.forEach(function (r, i) {
r[1] == type ? arr.push(r) : rowsToWriteBack.push(r)
});
if (arr.length > 0) {
targetSheet.getRange(targetSheet.getLastRow() + 1, 1, arr.length, arr[0].length)
.setValues(arr);
sheet.clear();
sheet.getRange(1, 1, rowsToWriteBack.length, rowsToWriteBack[0].length)
.clear()
.setValues(rowsToWriteBack);
}
}
除了: 我当前设置需要更改的原因是因为我的表单太大(多节表单)提交了很多列(A-CH),每个条目只提交大约8个(只是在不同的列中),即使是隐藏列,我在移动设备上查看它在50%的时间内冻结/崩溃的页面(我不确定这会完全解决它,因为有很多行的工作表仍然在主工作表上,请注意它将是空白的)。这让我有疑问,如果只有一张带有从未在移动设备上查看过的许多单元格的工作表导致工作表加载速度非常慢?
由于
修改
链接到包含表单/表单/脚本
的Drive文件夹https://drive.google.com/folderview?id=0B-7N8A7FPKw5V20yRVFQcmNZTlk&usp=sharing
EDIT2:
过去几天我一直试图解决这个问题,我有一个二级脚本,我已经半工作但下面标记的部分是我想要在原始公式中工作的
function onEdit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet2Copy = ss.getSheetByName('Actions');
var targetSheet = ss.getSheetByName('Test');
var data = sheet2Copy.getRange(1,1, sheet2Copy.getLastRow(), sheet2Copy.getLastColumn()).getValues();
var dest = [];
for (var i = 0; i < data.length; i++ ) {
Logger.log(data[i][12]);// just to check if the condition is true sometimes ;-)
if (data[i][1] == "Action") {
var rowsToWriteBack = []; // initialise intermediate array
// GET THIS WORKING IN ABOVE FORMULA
// Trying to select columns to put into new sheet. Will eventually make the #'s variables passed into the formula to make it dynamic
rowsToWriteBack.push(
data[i][0],
data[i][1],
data[i][2],
data[i][10],
data[i][11],
data[i][12],
data[i][13]
);// choose here the columns you want to add
dest.push(rowsToWriteBack);
}
} // here is the end of the for loop
Logger.log(dest) ; // log the dest array instead
if (dest.length > 0 ) { // if something has been written in your array then batch write it to the dest. sheet
targetSheet.getRange(targetSheet.getLastRow()+1,1,dest.length,dest[0].length).setValues(dest);
}
}
答案 0 :(得分:0)
检查一下! 这将检查每一行,并且只接受非空条目,并将结果放在工作表targetSheetName中。
function minimizeRows(sheetName,targetSheetName) {
// default sheetName is "test8" and default targetSheetName = "test-results";
sheetName = sheetName ? sheetName : "test8";
targetSheetName = targetSheetName ? targetSheetName : "test8-results";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheetName) ? ss.getSheetByName(sheetName) : ss.insertSheet(sheetName);
var values = sheet.getDataRange().getValues();
var headers = values.shift();
// this adds the column name after each entry
for(var i=0;i<values.length;i++) {
for(var k=0;k<values[i].length;k++) {
if(values[i][k])
values[i][k] = values[i][k] + " (column: " + headers[k] + ")";
}
}
// this filters out only the non-empty entries
for(var i=0;i<values.length;i++) {
for(var k=0;k<values[i].length;k++) {
if(!values[i][k]) {
values[i].splice(k,1);
k--; // important to decrease k, because the row values[i] is decreased in size by one by split(k,1)
}
}
}
// this loop makes sure that every row will become the same length (the max length of a row)
var maxLengthOfRow = Math.max.apply(Math,values.map(function(item) { return item.length; }));
for(var i=0;i<values.length;i++) {
for(var k=0;k<maxLengthOfRow;k++) {
if(!values[i][k])
values[i].push(""); // add empty entries to create the right length of the row
}
}
var targetSheet = ss.getSheetByName(targetSheetName) ? ss.getSheetByName(targetSheetName) : ss.insertSheet(targetSheetName);
targetSheet.getRange(targetSheet.getLastRow()+1,1,values.length,values[0].length).setValues(values);
}
答案 1 :(得分:0)
所以我无法直接在主要工作表“SortBot”上工作,因此SortBot将所有单元格写入其旁边的另一个工作表。然后是另一个脚本“moveToSheet”,它读取新工作表并将所有需要的列传输到新工作表。
我遇到的一个问题是发送到新表单的所有响应我无法监视更改并运行触发器。因此,当响应进来时,我将单元格C2设置为“未排序”,如果页面打开,则选择C2(下拉列表)并选择“排序”,然后它将对表格进行排序。
如果有人想知道系统的工作原理让我知道,我打算制作一个完整系统的视频演示,供其他人使用
// Main page running the code
var responseRange = "A2:CJ";
var targetSheetSortCell = "C2";
var targetSheetSortCellValue = "Not sorted";
var type1 = "Action";
var sheetName1 = "Actions";
var targetURL1 = "SHEET_URL";
var columnsToCopy1 = [0,1,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25];
moveToSheet(type1, sheetName1, targetURL1, columnsToCopy1, responseRange, targetSheetSortCell, targetSheetSortCellValue)
// The function
function moveToSheet(type, sheet, targetURL, columnsToCopy, responseRange, targetSheetSortCell, targetSheetSortCellValue) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet2Copy = ss.getSheetByName(sheet);
var target = SpreadsheetApp.openByUrl(targetURL)
var targetSheet = target.getSheetByName(sheet);
var columnsToCopy = columnsToCopy;
var responseRange = responseRange;
var targetSheetSortCell = targetSheetSortCell;
var targetSheetSortCellValue = targetSheetSortCellValue;
var data = sheet2Copy.getRange(1,1, sheet2Copy.getLastRow(), sheet2Copy.getLastColumn()).getValues();
var dest = [];
for (var i = 0; i < data.length; i++ ) {
if (data[i][1] == type) {
var destRow = []; // initialise intermediate array
for (var v = 0; v < columnsToCopy.length; v++ ) {
destRow.push(data[i][columnsToCopy[v]]);
}
dest.push(destRow);
targetSheet.getRange(targetSheetSortCell).setValue(targetSheetSortCellValue);
}
}
if (dest.length > 0 ) { // if something has been written in your array then batch write it to the dest
sheet2Copy.getRange(responseRange).clear();
targetSheet.getRange(targetSheet.getLastRow()+1,1,dest.length,dest[0].length).setValues(dest);
}
}