将数据从源工作表复制到目标工作表,如果数据已经存在,则覆盖目标工作表中的数据



主要目标是跟踪Source工作表中更新到Destination工作表中的数据。每周,过去两周的数据上传到表格1。这周是第46周和第45周上周是第45周和第44周Destination Sheet中的第45周应该被我们这周上传的覆盖,而不是上周加载的。第46周也应该列在清单上。使用以下函数将我的数据从源表传输到目标表。

function CopyDataToNewFile() {
var sss = SpreadsheetApp.openById('1BQb-_572SbReJWcD8nhfWFj8u4D15jhg7NtrsBT2vz4'); // sss = source spreadsheet
var ss = sss.getSheetByName('Source'); // ss = source sheet
var SData = ss.getDataRange().getValues();
var tss = SpreadsheetApp.openById('1BQb-_572SbReJWcD8nhfWFj8u4D15jhg7NtrsBT2vz4'); // tss = target spreadsheet
var ts = tss.getSheetByName('Destination'); // ts = target sheet
ts.getRange(ts.getLastRow()+1,1,SData.length,SData[0].length).setValues(SData);
}

我需要改变功能,所以它覆盖第45周。我还只想要Destination表上最近14周的数据,所以我必须删除最近16周之前的周。谢谢你的帮助。

这里是到Google工作表的链接:https://docs.google.com/spreadsheets/d/1BQb-_572SbReJWcD8nhfWFj8u4D15jhg7NtrsBT2vz4/edit?usp=sharing

试试下面的代码。…

  1. 从"来源"表中查找最近15周的数据,
  2. 清除"目的地"页的所有内容,
  3. 将在步骤1中找到的数据扔到空的'Destination'表中。

注意:您可以更改变量'limit'的值,以设置您希望将多少周的数据带入'Destination'工作表。

如果你想要一个目标工作表保持最新的数据更新,而不是检查整个目标工作表的旧数据被删除,并尝试插入不存在的新数据到正确的位置,

直接删除上面的所有内容,然后重新插入应该保留的数据,这总是一种更快、更容易、更可靠的方法。

function CopyDataToNewFile() {
const limit = 15;
const ssid = '1BQb-_572SbReJWcD8nhfWFj8u4D15jhg7NtrsBT2vz4';
const sss = SpreadsheetApp.openById(ssid);
const ss = sss.getSheetByName('Source');
const ts = sss.getSheetByName('Destination');
const sValues = ss.getDataRange().getValues();
const dates = sValues
.map(([year,week,...rest]) => [year,week].join(''))
.filter((n,i,s) => !isNaN(n) && i === s.indexOf(n))
.reverse()
.filter((v,i) => i<limit)
.map(v => [v.slice(0,4),v.slice(4)]);

const results = sValues.filter(([year,week,...rest],i) => {
return dates.some(([dYear,dWeek]) => year == dYear && week == dWeek) || i === 0;
});
ts.clearContents();
ts.getRange(1,1,results.length,results[0].length).setValues(results);
}

但是!

如果你的源表不总是包含所有时间的数据,那就行不通了。

在这种情况下,尝试第二次尝试:

  1. 这段代码比较了'源'和'目的'表的'年'和'周',
  2. 获取具有年份&
  3. 用添加的新条目重新创建'Destination'表的值,
  4. 删除超过设置的"限制"周数的旧值,
  5. 清除目标表,
  6. setValues back to target sheet
function CopyDataToNewFile() {
const limit = 15;
const getDates = (values) => {
return values
.map(([year,week,...rest]) => [year,week].join(''))
.filter((n,i,s) => !isNaN(n) && i === s.indexOf(n))
.reverse()
}
const ssid = '1BQb-_572SbReJWcD8nhfWFj8u4D15jhg7NtrsBT2vz4';
const sss = SpreadsheetApp.openById(ssid);
const ss = sss.getSheetByName('Source');
const ts = sss.getSheetByName('Destination');
const sValues = ss.getDataRange().getValues();
const tValues = ts.getDataRange().getValues(); // < typo, was ss.getDataRange(), it should be ts.getDataRange() instead.
const sDates = getDates(sValues);
const tDates = getDates(tValues);
const nDates = sDates.filter(n => n > Math.max(...tDates));
const aDates = tDates.concat(nDates).filter((val,i) => i<limit);
const results = [];
for (const [year,week,...rest] of sValues) {
if (nDates.includes([year,week].join(''))) tValues.push([year,week,...rest]);
}
for (const [i,[year,week,...rest]] of tValues.entries()) {
if (i === 0 || aDates.includes([year,week].join(''))) results.push([year,week,...rest]);
}
ts.clearContents();
ts.getRange(1,1,results.length,results[0].length).setValues(results);
}

也许在比较之前对日期值进行排序可以解决问题,我还重新安排了一些变量,以便更容易访问和更新:

function CopyDataToNewFile2() {
const limit = 15; // limit of week counts to be returned
const ssid = '1BQb-_572SbReJWcD8nhfWFj8u4D15jhg7NtrsBT2vz4'; // id of the spreadsheet
const source = 'Source' // Source sheet name;
const destination = 'Destination' // Destination sheet name;
const getDates = (values) => {
return values
.map(([year,week,...rest]) => [year,week].join(''))
.filter((n,i,s) => !isNaN(n) && i === s.indexOf(n))
}
const sss = SpreadsheetApp.openById(ssid);
const ss = sss.getSheetByName(source);
const ts = sss.getSheetByName(destination);
const sValues = ss.getDataRange().getValues();
const tValues = ts.getDataRange().getValues(); // < typo, was ss.getDataRange(), it should be ts.getDataRange() instead.
const sDates = getDates(sValues);
const tDates = getDates(tValues);
const nDates = sDates.filter(n => n > Math.max(...tDates));
const aDates = tDates.concat(nDates).sort((a,b) => b - a).filter((val,i) => i<limit);
const results = [];
for (const [year,week,...rest] of sValues) {
if (nDates.includes([year,week].join(''))) tValues.push([year,week,...rest]);
}
for (const [i,[year,week,...rest]] of tValues.entries()) {
if (i === 0 || aDates.includes([year,week].join(''))) results.push([year,week,...rest]);
}
ts.clearContents();
ts.getRange(1,1,results.length,results[0].length).setValues(results);
}

不只是复制源工作表中的新条目,如果给定数据的日期出现在源工作表中,此代码还将替换目标工作表中的值:

function CopyDataToNewSheet() {
const limit = 17; // limit of week counts to be returned
const ssid = '1Yf6RItkwa6T1pZEt-RFZRdapdy2NfoFgKUCMxqPbSD4'; // id of the spreadsheet
const source = 'Source' // Source sheet name;
const destination = 'Destination' // Destination sheet name;
const getDates = (values) => {
return values
.map(([year,week,...rest]) => [year,week].join(''))
.filter((n,i,s) => !isNaN(n) && i === s.indexOf(n))

}
const sss = SpreadsheetApp.openById(ssid); //open the spread sheet
const ss = sss.getSheetByName(source);
const ts = sss.getSheetByName(destination);
const sValues = ss.getDataRange().getValues();
const tValues = ts.getDataRange().getValues();
const sDates = getDates(sValues);
const tDates = getDates(tValues).sort((a,b) => b - a);
const oDates = tDates.filter(d => !sDates.includes(d)).filter((v,i) => i<(limit-sDates.length));
tValues.shift();
for (const [i,[year,week,...rest]] of tValues.entries()) {
if (!!i && oDates.includes([year,week].join(''))) sValues.push([year,week,...rest]);
}
const results = sValues.sort((a,b) => [b[0],b[1]].join('') - [a[0],a[1]].join(''));
ts.clearContents();
ts.getRange(1,1,results.length,results[0].length).setValues(results);
}

最新更新