如何使用Google脚本将数据添加到Google表格中的特定行 [英] How to add data to a specific row in Google Sheets using Google Script

查看:435
本文介绍了如何使用Google脚本将数据添加到Google表格中的特定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Google表格中,我有一个名为colorRow的变量。我只是试图向表格中的那一行添加一些数据,但数据一直沿着页面向下。我究竟做错了什么?

In my Google Sheet, I have the index in a variable called colorRow. And i'm just trying to add some data to that row in the sheet but the data keeps going to some other row down the page. What am I doing wrong?

var data = [];
// Some more fields in data array
data.push("Final Submission Date: " + new Date());
// target_sheet.insertRowAfter(colorRow); // Tried this didn't work
target_sheet.appendRow(data);

我需要找出某种方法来重置追加计数器,以便我可以将其设置为行{colorRow}或者我可以添加到特定行的方式,可能类似于

I need to figure out some way to either reset the append counter so that I can set it to row {colorRow} or if there is a way I can add to a specific row, maybe something like

target_sheet.addAt(7, data)

像这样的东西将是理想的。

Something like this would be ideal.

推荐答案

你无法使用appendRow来做你想做的事。您正在使用:

You can not use appendRow to do what you want. You are using:

target_sheet.appendRow(data);

您需要使用 setValues()

target_sheet(start row, start column, number of rows, number of columns).setValues(data);

在您的情况下,如果您想覆盖(编辑)现有的行,那么您需要找到其中包含索引的行,并将该数字用于开始行参数。

In your case, if you want to overwrite (edit) an existing row, then you will need to find the row with the index in it, and use that number for the start row parameter.

var data, dataAs1D,dataFromCol1,ouss, target_sheet, lastRow,rowToEdit;

ss = SpreadsheetApp.getActiveSpreadsheet();
target_sheet = ss.getSheetByName('name');

lastRow = target_sheet.getLastRow();

dataFromCol1 = target_sheet.getRange(1,1,lastRow,1).getValues();//Get all col A values
dataAs1D = dataFromCol1.toString().split(",");//Convert 2D array to 1D

rowToEdit = colorRow;//

data = ['one','two','three'];
outerArray = [];//Assign empty array to variable name
outerArray.push(data);
target_sheet.getRange(rowToEdit,1,data[0].length,data.length).setValues(data);

这个例子假设您的索引号在第一列。

This example assumes that your index numbers are in column one.

这篇关于如何使用Google脚本将数据添加到Google表格中的特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆