無法使代碼正常作業,我想檢查電子表格中的每個單元格的值并粘貼包含以下值的整行。
var rowValue = spreadSheetData.getValues()
var findText = spreadSheetData.createTextFinder(usedEmail)
var bool
var mailFinder = findText.findAll().map(x => x.getA1Notation())
Logger.log(mailFinder)
if (choice == "Sí") {
var i = mailFinder.indexOf(usedEmail)
if (i != "") {
rowValue[i][0] = new Date()
var rowValues = rowValue[i]
bool = true
}else{
bool = false
}
}
如果我確實喜歡上面的代碼,它可以作業并根據結果給我真或假,但“i”變數會拋出“未定義”,因為我沒有使用 rowValue 變數。另一方面,如果我添加一個“for”迭代,例如:
for(i = 0; i < rowValue.length; i ){
rowValue[i][0] = new Date()
var rowValues = rowValue[i]
}
它復制最后一行,這不是我想要的方法,因為我想復制值所在的行。
Example sheets:
| header1 | header2 |
| -------- | ------------------------- |
| 231232132| valueiwanttofind@gmail.com|
| asdasdas | row |
| header1 | header2 |
| ------------------------- | ------------------------- |
| 231232132 | row |
| valueiwanttofind@gmail.com| another row |
[email protected] 可以在任何單元格中。我嘗試了另一種方法,但沒有運氣......
for(var i = 1; i < rowValue.length; i ) {
for(var n = 1; n < rowValue[i].length; n ) {
var findText = rowValue[i]
Logger.log(findText)
if (choice == "Sí") {
if (findText.indexOf(usedEmail)) {
rowValue[i][0] = new Date()
var rowValues = rowValue[i]
bool = true
}else{
bool = false
}
}
}
}
更新:
感謝您的幫助,我設法實作了我所需要的,我會簡要解釋一下,如果有人遇到同樣的問題
function getItems(email,usedEmail, choice, name, gender){ //This functions brings vars as parameters from main function who map every row
var spreadSheet = SpreadsheetApp.openById(form.getDestinationId())
var spreadSheetData = spreadSheet.getDataRange()
var rowValue = spreadSheetData.getValues()
var bool
rowResult = rowValue.find(r => r.includes(usedEmail)) //I didn't know how to use find, instead i used findAll with textfinder
if (choice == "Sí") {
if (rowResult) {
var rowValues = rowResult //pass rowValues with the result above
bool = true
}else{
bool = false
}
}
for(i = 0; i < rowValue.length; i ){
rowValue[i][0] = new Date() //I used just in case to put a the current date when the form is sent to not copy that value(overwrite).
}
Logger.log(usedEmail)
Logger.log(bool)
if (bool == true){
setItems(spreadSheet, rowValues)
//RespuestaAutomatica(name, email, gender) is another function i have
Logger.log("Inscripción válida")
}else{
Logger.log("Inscripción fallida, email no registrado")
}
}
function setItems(spreadSheet, rowValues){ //the function created to append the rows at the next one.
spreadSheet.appendRow(rowValues)
}
謝謝你的幫助!
uj5u.com熱心網友回復:
建議:
盡管仍不清楚line of code下面的具體目的是什么,但我預感您的主要目標是獲取在您的作業表上找到匹配項的行號。但是,如果我們誤解了您的帖子,請隨時告訴我們。
不清楚的代碼行:
rowValue[i][0] = new Date()
var rowValues = rowValue[i]
為什么要在陣列資料中分配日期值
rowValue[i][0]?這樣做的具體目的是什么?
也許你可以試試這個調整后的腳本:
//Initialized these lines below for the code to work
var spreadSheetData = SpreadsheetApp.getActiveSpreadsheet();
var usedEmail = "[email protected]";
var choice = "Sí";
function testFunction() {
var rowValue = spreadSheetData.getDataRange().getValues();
var findText = spreadSheetData.createTextFinder(usedEmail);
var bool;
var mailFinder = findText.findAll().map(x => x.getA1Notation().toString())
if (choice == "Sí" & mailFinder.length != 0) {//Make sure mailfinder is not null
var rowValues = mailFinder;
rowValues = rowValues.map(function(x){ return x.replace(/[^abc]/,'') }); //Get the row numbers of the macthed rows that are orginally in A1Notation format
Logger.log("Found \"" "" usedEmail "\" on Row #:\n" rowValues);
bool = true;
}else{// If mailFinder has no data found
bool = false
}
Logger.log(bool);
}
樣本結果:

我用過的測驗表:
uj5u.com熱心網友回復:
像這樣的東西?
function myFunction() {
var email = '[email protected]'; // the wanted email
var sheet = SpreadsheetApp.getActiveSheet(); // the current sheet
var data = sheet.getDataRange().getValues(); // all the data from the sheet
var row = data.find(r => r.includes(email)); // the row with the email or undefined
if (row) sheet.appendRow(row); // append the row at the end of the sheet
}
它在作業表末尾復制(附加)包含電子郵件的第一行。
(如果你想要行的索引,你可以使用findIndex()代替find())
如果要附加包含電子郵件的所有行,您可以使用以下命令filter()代替find():
function myFunction() {
var email = '[email protected]'; // the wanted email
var sheet = SpreadsheetApp.getActiveSheet(); // the current sheet
var data = sheet.getDataRange().getValues(); // all the data from the sheet
var rows = data.filter(r => r.includes(email)); // the rows with the email
// append the array of rows at the end of the sheet
if (rows) rows.forEach(row => {
sheet.appendRow(row); // <--- it's ok if there are a few rows only
SpreadsheetApp.flush();
});
}
(如果電子郵件中有很多行,則應該改進腳本)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/410889.html
標籤:
上一篇:當url為空時如何跳過UrlFetchApp并仍然運行腳本
下一篇:使用Google表格中的陣列以html格式創建資料串列,該串列可提供給GoogleApps腳本以自動完成文本輸入

