【发布时间】:2019-03-06 20:32:10
【问题描述】:
我有一个 Google 电子表格,其中包含主工作表上的论文名称(称为“摘要”)。其中一列指示论文是否已在实验室中讨论过(“si”、“no”)。当列显示“si”(是)时,脚本将论文名称和信息复制到另一个名为“Seminarios”(研讨会)的工作表中,如果列显示“否”,则将信息复制到另一个名为“DISCUTIR”的工作表(讨论)。该脚本工作正常(它将信息复制到相应的工作表),但它有一个小问题。当我们讨论一篇论文并且我将其状态从“否”更改为“是”时,它会将其添加到 SEMINARIO 表中,但不会将其从“DISCUTIR”表中删除。有没有办法让脚本刷新整个工作表? (如果我删除“DISCUTIR”表的全部内容,它只会添加我想要的内容,但我想找到一种自动执行此操作的方法)。
在这里我附上我正在使用的代码。我确信它真的很不整洁而且效率低下,因为我不知道我在做什么,所以我为此道歉。我有一些 MATLAB 编程经验,但从未使用过 JavaScript,只是设法通过复制和粘贴代码使其工作。
function onEdit(e) {
var sheetFrom = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("ABSTRACTS");
var sheetTo = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SEMINARIOS");
var sheetTo2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("DESCARTADOS");
var sheetTo3 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("DISCUTIR");
var sheetTo4 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("CHEQUEAR");
var data = sheetFrom.getDataRange().getValues();
var target = new Array(); // this is a new array to collect data
for (n = 0; n < data.length; ++n) { // iterate in the array, row by row
if (data[n][8] == "si") { // if condition is true copy the whole row to target
target.push(data[n]); // copy the whole row
} //if
} //for
//Paste to another sheet from first cell onwards
sheetTo.getRange(2, 1, target.length, target[0].length).setValues(target);
var data = sheetFrom.getDataRange().getValues();
var target = new Array(); // this is a new array to collect data
for (n = 0; n < data.length; ++n) { // iterate in the array, row by row
if (data[n][8] == "descartado") { // if condition is true copy the whole row to target
target.push(data[n]); // copy the whole row
} //if
} //for
//Paste to another sheet from first cell onwards
sheetTo2.getRange(2, 1, target.length, target[0].length).setValues(target);
var data = sheetFrom.getDataRange().getValues();
var target = new Array(); // this is a new array to collect data
for (n = 0; n < data.length; ++n) { // iterate in the array, row by row
if (data[n][8] == "no") { // if condition is true copy the whole row to target
target.push(data[n]); // copy the whole row
} //if
} //for
//Paste to another sheet from first cell onwards
sheetTo3.getRange(2, 1, target.length, target[0].length).setValues(target);
var data = sheetFrom.getDataRange().getValues();
var target = new Array(); // this is a new array to collect data
for (n = 0; n < data.length; ++n) { // iterate in the array, row by row
if (data[n][8] == "chequear") { // if condition is true copy the whole row to target
target.push(data[n]); // copy the whole row
} //if
} //for
//Paste to another sheet from first cell onwards
sheetTo4.getRange(2, 1, target.length, target[0].length).setValues(target);
}
【问题讨论】:
标签: javascript google-apps-script google-sheets conditional refresh