简体   繁体   English

使用谷歌脚本填充谷歌表格中的特定列

[英]Populate specific column in google sheet using google script

I'm using google script in google sheet in order to populate the cell with data that I entered.我在谷歌表中使用谷歌脚本,以便用我输入的数据填充单元格。 First I made a form by using html where I store data I entered as object in html (image below).首先,我使用 html 制作了一个表格,其中我将输入为 object 的数据存储在 html 中(下图)。 在此处输入图像描述

then i call the data I stored in html code with javascript in order to populate the targeted sheet(the last row of the first column)(image below), and it worked.然后我用 javascript 调用我存储在 html 代码中的数据,以填充目标工作表(第一列的最后一行)(下图),它起作用了。

在此处输入图像描述

But then, I created the same form with the same code html but for now I want to populate the last row of the second column.但是,我用相同的代码 html 创建了相同的表单,但现在我想填充第二列的最后一行。 I'm trying using the same code appendRow ([null,data.name]), but the problem is it will populate the last row of the sheet.我正在尝试使用相同的代码appendRow ([null,data.name]),但问题是它将填充工作表的最后一行。 I tried to use getRange then setValues but it doesnt work.我尝试使用getRange然后setValues但它不起作用。

Please help请帮忙

Try changing the last row in 'appenData' to尝试将“appenData”中的最后一行更改为

ws.getRange(ws.getLastRow() + 1, 2).setValue(data.name);

You can refer to this sample code:你可以参考这个示例代码:

function appendToColumn() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("trial");
  var data = "Testing";
  //Get the column where you want to append the data
  var range = sheet.getRange("B1:B").getValues().flat();
  var row = range.filter(String).length;
  Logger.log(row);
  sheet.getRange(row+1,2).setValue(data);
}

What it does?它能做什么?

  1. Get the range of the column where you want to append your data获取您想要 append 数据的列的范围
  2. Get its value, use array.flat() to change 2-d array to 1-d array获取它的值,使用array.flat()将二维数组更改为一维数组
  3. Remove empty cell values using array.filter() and get its length使用array.filter()删除空单元格值并获取其长度
  4. The length of the array should match the row count of your desired column数组的长度应与所需列的行数相匹配
  5. Increment the current row count and set its value增加当前行数并设置其值

Output: Output:

Before:前:

在此处输入图像描述

After:后:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Google脚本使用数组填充Google表格行-范围宽度不正确 - Populate Google Sheet row with an array using Google Script – Incorrect range width 如何让 Snipe-IT API 使用 Google 应用脚本检索数据并将其填充到 Google 表格中? - How do I get Snipe-IT API to retrieve data using a Google App Script and populate it on a Google Sheet? 如何使用 Google 脚本从 1 个 Google 工作表中复制特定值并粘贴到另一个工作表? - How can I copy specific values from 1 Google Sheet and paste to another Sheet using Google Script? 使用 Apps 脚本自动化 Google 表格中列的日期 - Automating date of a column in Google Sheet using Apps Script 使用谷歌工作表脚本在列中查找空单元格 - Finding an empty cell in a column using google sheet script Google Sheet Script:如何在新专栏中自动更新Google Sheet' - Google Sheet Script : How to autoupdate Google Sheet 'in new column' Google脚本:如何将Google工作表上的数组值设置为特定的行/列 - Google script: how to set Array Value on google sheet to specific rows/column Google Sheet Script - 删除包含特定数据的行 - Google Sheet Script - remove row with specific data Google表格脚本可为特定单元格添加时间 - Google Sheet Script To Add Time To Specific Cell 使用谷歌应用脚本将谷歌工作表中的数据拆分到另一张工作表 - Split data in google sheet to another sheet using google app script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM