Google 应用脚本:在 Google 表格中动态添加新行
Google App Script: Dynamically add new rows in Google Sheets
我正在 sheet 构建要导入 Shopify 的产品列表。
为此,我有一些基本数据(此处无关紧要)的 pdf 格式,我从中构建了一个字符串来抓取产品供应商的网站,并以适合在 Shopify 中导入的方式格式化数据。
产品有不同数量的图像 (1 - 8),所以我正在尝试构建我的脚本,如果产品有多个图像,我会尝试在其下添加额外的行并将第一张图像之后的每个图像添加到新行中.
这是我的代码:
function iterateThroughRows() {
// get spreadsheet
const sheet = SpreadsheetApp.getActive().getSheetByName("MySheet");
const data = sheet.getDataRange().getValues();
// Loop over rows
data.forEach( (row, rowIndex) => {
const imageSrcArray = [ /* list of image URLs, fetched from remote server */ ]
imageSrcArray.forEach( (img, arrayIndex) => {
if(arrayIndex == 0) { // for the first array item, just add it to the current row
const imageCell = sheet.getRange(rowIndex + 1, 24)
imageCell.setValue( imageSrcArray[arrayIndex] )
} else { // for each array item past the first one, add a new row and enter the value there
sheet.insertRows(rowIndex)
const imageCell = sheet.getRange(rowIndex + arrayIndex + 1, 24)
imageCell.setValue( imageSrcArray[arrayIndex] )
}
})
// adding some more values to other cells
});
}
实际上这并没有用。
我昨天一整天都在处理这个问题,并且有一个使用 insertRowAfter()
的版本确实添加了额外的行,但是将它们全部集中在一起(即第一个产品之后会有 15 行,但是 none 在任何其他人之后)。但是由于 Google App Script 没有版本控制,我丢失了那个版本。
我认为问题是 forEach
似乎移到新创建的行并不断从那里添加内容,而不是移到最初的下一行。
所以我或多或少对此感到不知所措。任何有关如何正确执行此操作的建议都将不胜感激。
我能理解你的沮丧,这确实是因为你关心根据 sheet 版本 在 添加新行之前计算行。
所以我的建议是这样做,因为 currentRow
允许您跟踪您正在处理的当前行。我还更新了 insertRowAfter()
,因为我认为这是您真正想要做的。
let currentRow = 1;
data.forEach( (row, rowIndex) => {
const imageSrcArray = [ "img1URL", "img2URL"]
if( !imageSrcArray.length ) return
imageSrcArray.forEach( (img, arrayIndex) => {
if( arrayIndex == 0 ){
sheet.getRange(currentRow, 24).setValue( img )
} else {
sheet.insertRowAfter(currentRow)
sheet.getRange(currentRow+1, 24).setValue( img )
}
// New rows in between were created
currentRow++
})
});
我正在 sheet 构建要导入 Shopify 的产品列表。
为此,我有一些基本数据(此处无关紧要)的 pdf 格式,我从中构建了一个字符串来抓取产品供应商的网站,并以适合在 Shopify 中导入的方式格式化数据。
产品有不同数量的图像 (1 - 8),所以我正在尝试构建我的脚本,如果产品有多个图像,我会尝试在其下添加额外的行并将第一张图像之后的每个图像添加到新行中.
这是我的代码:
function iterateThroughRows() {
// get spreadsheet
const sheet = SpreadsheetApp.getActive().getSheetByName("MySheet");
const data = sheet.getDataRange().getValues();
// Loop over rows
data.forEach( (row, rowIndex) => {
const imageSrcArray = [ /* list of image URLs, fetched from remote server */ ]
imageSrcArray.forEach( (img, arrayIndex) => {
if(arrayIndex == 0) { // for the first array item, just add it to the current row
const imageCell = sheet.getRange(rowIndex + 1, 24)
imageCell.setValue( imageSrcArray[arrayIndex] )
} else { // for each array item past the first one, add a new row and enter the value there
sheet.insertRows(rowIndex)
const imageCell = sheet.getRange(rowIndex + arrayIndex + 1, 24)
imageCell.setValue( imageSrcArray[arrayIndex] )
}
})
// adding some more values to other cells
});
}
实际上这并没有用。
我昨天一整天都在处理这个问题,并且有一个使用 insertRowAfter()
的版本确实添加了额外的行,但是将它们全部集中在一起(即第一个产品之后会有 15 行,但是 none 在任何其他人之后)。但是由于 Google App Script 没有版本控制,我丢失了那个版本。
我认为问题是 forEach
似乎移到新创建的行并不断从那里添加内容,而不是移到最初的下一行。
所以我或多或少对此感到不知所措。任何有关如何正确执行此操作的建议都将不胜感激。
我能理解你的沮丧,这确实是因为你关心根据 sheet 版本 在 添加新行之前计算行。
所以我的建议是这样做,因为 currentRow
允许您跟踪您正在处理的当前行。我还更新了 insertRowAfter()
,因为我认为这是您真正想要做的。
let currentRow = 1;
data.forEach( (row, rowIndex) => {
const imageSrcArray = [ "img1URL", "img2URL"]
if( !imageSrcArray.length ) return
imageSrcArray.forEach( (img, arrayIndex) => {
if( arrayIndex == 0 ){
sheet.getRange(currentRow, 24).setValue( img )
} else {
sheet.insertRowAfter(currentRow)
sheet.getRange(currentRow+1, 24).setValue( img )
}
// New rows in between were created
currentRow++
})
});