我使用2个Google电子表格,它们基本上执行相同的操作,但是其中包含不同的数据,因此,每次更新一个电子表格时,我都必须对另一个电子表格进行相同的更新。有时更改很复杂,所以我认为我会自动将更改从“父工作表”复制到子级。通过查找具有相同名称的列并将它们移动到应有的位置,以及通过在相同标题下用父公式替换子公式,最后将子表中的数据值复制到它们所属的新列中,该方法起作用。 / p>
我已经达到了某种程度的效果,但是问题是复制了其余的单元格内容。
如果我在电子表格的工作表之间移动数据,我只能将源范围复制到其他范围,但是当在电子表格之间移动数据时,因为没有copyRangeBetweenSpreadsheets函数,所以我必须将所有内容都移动到变量中。首先是公式,然后是值,然后是数据验证,然后是格式和条件格式。它将变得非常缓慢,并且当子电子表格变大时最终会超时。
是否有快捷方式可以将所有数据加载到单个容器中以简化流程?
已编辑以添加代码。在这里:
function updateChildSheets(){
updateChildSheet('13_8O0xMjy_9KLGEWkfXpyIrG88bZ9Yr25eu7DRHC_V4','Links',1);
}
function updateChildSheet(childSID,sheet,ParentHeaderRow) { //Parent Header row is the row containing the heading names for each column and is not necessarily thie first row of the sheet
//sheet is the sheet name inside the Parent and Child sheets to update
var ss = SpreadsheetApp.getActiveSpreadsheet();
var parentSheet = ss.getSheetByName(sheet);
//var childSID = Browser.inputBox('Remote Sheet to Update');
var childSS = SpreadsheetApp.openById(childSID);
var childSheet = childSS.getSheetByName(sheet);
var childSheetMaxCols = childSheet.getMaxColumns();
var childSheetMaxRows = childSheet.getMaxRows();
var childSheetHeadings = childSheet.getRange(ParentHeaderRow+':'+ParentHeaderRow).getValues();
var parentSheetHeadings = parentSheet.getRange(ParentHeaderRow+':'+ParentHeaderRow).getValues();
var parentSheetMaxCols = parentSheet.getMaxColumns();
var parentSheetMaxRows = parentSheet.getMaxRows();
var parentSheetFormulas = parentSheet.getRange(1,1,parentSheetMaxRows,parentSheetMaxCols).getFormulas();
var virtualSheetArr = [];
//Create the new sheet headings
var headingRow =[];
for (var i in parentSheetHeadings[0]) headingRow.push(parentSheetHeadings[0][i]);
// create the correct length (the number of rows in the child sheet)
var dummyRow=[];
virtualSheetArr.push(headingRow);
//create empty rows equal to the length of data in child sheet
for (var i in virtualSheetArr[0]) dummyRow.push('');
for (var i=0; i<childSheetMaxRows-1; i++) {
virtualSheetArr.push(dummyRow.slice())
}
//find childHeading and copy rows below into VirtualArr
//for each column in source sheet
Loop1:
for (var headingCol=0; headingCol<virtualSheetArr[0].length; headingCol++){
//Locate column in the child sheet (childSheetCol)
var columnFound=false;
for (var childSheetCol=0; childSheetCol<childSheetMaxCols; childSheetCol++){
//if the column in child sheet matches the column in source sheet
if (virtualSheetArr[0][headingCol] == childSheetHeadings[0][childSheetCol]) {
columnFound=true;
break;
}
}
if(!columnFound) continue;
//get both formulas and values from child sheet and save them to a 2D array using identical columns from the parent Sheet
var childColValues = childSheet.getRange(1,childSheetCol+1,childSheetMaxRows,1).getValues();
var childColFormulas = childSheet.getRange(1,childSheetCol+1,childSheetMaxRows,1).getFormulas();
for(var i=1; i<childColValues.length;i++){
if(childColFormulas[i][0]!='') { //If a parent cell comtains a formula, save the new parent formula to the target cell
virtualSheetArr[i][headingCol] = parentSheetFormulas[i][headingCol];
//if a cell contains Arrayformula() remove any values found below it
if(/arrayformula/i.test(virtualSheetArr[i][headingCol])){
for(var j=i+1; j<childColValues.length; j++){
childColValues[j][0]='';
}
}
}
else if(childColValues[i][0]!='') virtualSheetArr[i][headingCol] = childColValues[i][0];
}
}
//Temporarily place the output in a test sheet to view output
var testsheet = childSS.getSheetByName('testsheet');
testsheet.clearContents().getRange(ParentHeaderRow,1,virtualSheetArr.length,(virtualSheetArr[0].length)).setValues(virtualSheetArr);
if(ParentHeaderRow>1) {
var parentSheetOtherHeadings = parentSheet.getRange(1+':'+(ParentHeaderRow-1))
testsheet.getRange(1,1,parentSheetOtherHeadings.length,parentSheetOtherHeadings[0].length).setValues(parentSheetOtherHeadings);
}
}