所以我有一个脚本(谷歌应用程序脚本),从我的一张纸张(到配对:姓名首字母和百分比)中提取数据,这些数据具有不断变化的价值(有时它只会在其他时间每周一次'每日)。
它应该根据新值检查旧值并仅处理新值,但由于某种原因它会处理所有值。
在循环过程中,它首先查找附加到该单元格的电子邮件,然后将生成的电子邮件发送给此人。然后在最后它存储上一个找到的新值。
获取新数据&变量
var data = dataRange.getValues(); // Fetch values for each row in the Range.
var oldData = [{}];
//Declare variable
从文档属性中获取旧数据。
var oldValues = PropertiesService.getDocumentProperties().getProperties();
//get values from document properties
var outerArrayOldData = [];
//empty array
var arr4 = [];
//empty array
var thisLoopString,
thisRowArray;
for (var key in oldValues) {
//grabbing keys from document properties 'row[i]' and loop for each
thisLoopString = oldValues[key];
thisRowArray = []; //Reset
array
thisRowArray = thisLoopString.split(","); //Convert the string to partial array
arr4.push(thisRowArray); //Push the inner array into the outer array
outerArrayOldData = arr4.concat(outerArrayOldData); //convert outer to actual usable array
var arr4 = []; //reset arr4 back to 0
};
//End getting old data
将旧数据与新数据进行比较
var oldData = outerArrayOldData;
var source = oldData.map(function (row) {
return JSON.stringify(row);
//map array to string
}),
searchRow,
dataLength = data.length;
for (i = 0; i < dataLength; i += 1) {
searchRow = JSON.stringify(data[i]);
if (source.indexOf(searchRow) == -1) {
//search old data and compare to new data using index search and if data isn't in old stack process it through functions
//doing stuff with new pairs
}
}
}
}
将旧数据存储到Doc属性。
var objOldData = {};
//empty
var keyName = "",
//empty
thisRowArray;
for (i = 0; i < data.length; i++) {
keyName = "row" + (i).toString();
//set keys
thisRowArray = data[i].toString();
//convert each pair array to string
if (thisRowArray == "") continue;
//skip blanks
objOldData[keyName] = thisRowArray;
//add keys and values to properties as a string
}
PropertiesService.getDocumentProperties().setProperties(objOldData,
true); //true deletes all other properties
//Store the Updated/New Values back to Properties
}
记录器控制台:
<<<<<<<<Imported Range data>>>>>>>>
[[BBB, 0.9], [CCC, 0.76], [DDD, 0.89], [, ]]
<<<<<<<<Old data from dpcument properties>>>>>>>>
[[DDD, 0.89], [, ], [BBB, 0.9], [CCC, 0.76]]
<<<<<Processing New Values Not in Old Data>>>>>
[CCC, 0.76]
[BBB, 0.9]
[DDD, 0.89]
<<<<<<<<Store the Updated/New Values back to Properties>>>>>>>>
{row1=CCC,0.76, row0=BBB,0.9, row3=,, row2=DDD,0.89}
正如您所看到的那样,它仍处理所有值,即使它们不是新的并且已经存在于系统中。为什么搜索没有发现它们已经存在?我在这方面出错了吗?
答案 0 :(得分:1)
我无法理解您的代码,因此我创建了自己的代码:
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var data = {};
function getData() {
var range = sheet.getRange("A1:B3");
var values = range.getValues();
for (var i=0; i < values.length; i++) {
var key = 'row' + i;
var currentRow = values[i];
// for each cell value,
// toString : convert to string
// trim : remove all whitespaces from both ends of cell values
// encode… : encode the values so we don't have any ","
var arr = currentRow.map(function(v){return encodeURIComponent(v.toString().trim())});
// join the array with "," delimiter
var s = arr.join();
data[key] = s;
}
} // getData()
function saveData() {
getData();
PropertiesService.getDocumentProperties().setProperties(data);
}
function compareData() {
getData();
var props = PropertiesService.getDocumentProperties().getProperties();
for (var idx in props) {
if (idx in data) {
if (data[idx] != props[idx]) {
Logger.log('\n%s is different\nOld value is "%s"\nNew value is "%s"',
idx,
decodeURIComponent(props[idx]),
decodeURIComponent(data[idx]));
}
} else {
Logger.log('missing row: ' + idx);
}
}
}
// Test function. Check all document properties
function peekProperties() {
var props = PropertiesService.getDocumentProperties().getProperties();
for (var idx in props) {
Logger.log('%s = %s', idx, props[idx]);
}
}
问题:删除行怎么办? key
不应该是A列中的值而不是行号吗?
答案 1 :(得分:1)
在循环代码的“将旧数据与新数据进行比较”中,尝试更改:
searchRow = JSON.stringify(data[i]);
到:
searchRow = JSON.stringify([data[i][0], data[i][1].toString()]);
这可确保第二个数组索引处的值始终转换为字符串,以便与“旧”导入值进行比较,该值似乎是从作为字符串传递的行中解析的。
当前看起来,新的数据数组值使用第二个值声明为数字(或者可能为null或空值):
[["BBB", 0.9], ["CCC", 0.76], ["DDD", 0.89], ["",""]];
虽然“旧”行(从Google doc导入)被导入并转换为数组,其中值为字符串:
[["CCC","0.76"],["BBB","0.9"],["",""],["DDD","0.89"]]
比较行与JSON.stringify,例如,'[“DDD”,“0.89”]'与'[“DDD”,0.89]'不匹配,因此所有行都被错误地注册为“new”。
我从你的例子中做了一些猜测到达到这个,但它可能是你的bug的原因。祝你好运!