简体   繁体   English

谷歌表格脚本,从不同的电子表格中获取和设置数据

[英]google sheets script, get and set data from different spreadsheets

I'm doing this as part of the internship.我这样做是实习的一部分。 I found here most part of what I needed but still can't complete it because I'm lacking in programmation.我在这里找到了我需要的大部分内容,但仍然无法完成它,因为我缺乏编程。

I have 3 SpreadSheets with their IDs:我有 3 个带有 ID 的电子表格:

  • SpreadSheet1 has some data stored in it, SpreadSheet1 中存储了一些数据,
  • SpreadSheet2 is the one where the script is running on, SpreadSheet2 是运行脚本的地方,
  • SpreadSheet3 is a backup of SpreadSheet2. SpreadSheet3 是 SpreadSheet2 的备份。

In SpreadSheet2 when I imput something in column A or B the script should look for that value on column A of SpreadSheet1, if the value is the same than it has to copy the corresponding row right after the input (from A to Z if or from B to Z, depending on who got the match) and delete that row from SpreadSheet1.在 SpreadSheet2 中,当我在 A 或 B 列中输入某些内容时,脚本应该在 SpreadSheet1 的 A 列上查找该值,如果该值相同,则必须在输入之后立即复制相应的行(从 A 到 Z,如果或从B 到 Z,取决于谁得到了匹配)并从 SpreadSheet1 中删除该行。 If the value isn't there than the imput should just be deleted.如果该值不存在,则应删除输入。

When i type 0 on column B in the Spreadsheet2 the script should copy all the data present there should on SpreadSheet3 (first row available and 0 should not be copied) than it should erase all rows except the first one from SpreadSheet2.当我在电子表格 2 的 B 列上键入 0 时,脚本应该复制电子表格 3 上应该存在的所有数据(第一行可用,不应该复制 0),而不是从电子表格 2 中删除除第一行之外的所有行。

Possibly this script should be available to use on different SpreadSheets simply by copying it, for example 3 computers should use different SpreadSheets with this script working on the same data from SpreadSheet1 and backuping all in SpreadSheet3.可能这个脚本应该可以通过简单地复制在不同的 SpreadSheet 上使用,例如 3 台计算机应该使用不同的 SpreadSheet,这个脚本处理来自 SpreadSheet1 的相同数据并在 SpreadSheet3 中备份所有数据。

Here is a visual example of what I'm trying to do这是我正在尝试做的一个视觉示例

Sorry for bad English and thanks in advance抱歉英语不好,提前致谢

function example1(e) {
      const range = e.range;
      const sheet = range.getSheet();
      if (sheet.getSheetName() != "Sheet1" || range.getColumn() != 1 || range.getRow() == 1) return;
      const value = range.getValue();
      const values = sheet.getRange("A2:A" + sheet.getLastRow()).getValues();
      values.splice(range.getRow() - 2, 1);
      if (values.flat().includes(value)) {
        range.clearContent();
        return;
      }
      const srcSheet = e.source.SpreadsheetApp.openById("SSheetID1");
      const srcSheet3 = e.source.SpreadsheetApp.openById("SSheetID3");
      const range3 = e.range;
      const sheet = range3.srcSheet3;
      const srcRange = srcSheet.getRange("A2:A" + srcSheet.getLastRow()).createTextFinder(value).matchEntireCell(true).findNext();
      if (srcRange) {
        srcRange.offset(0, 1, 1, srcSheet.getLastColumn() - 1).copyTo(range.offset(0, 1));
        srcRange.offset(0, 1, 1, srcSheet.getLastColumn() - 1).copyTo(range3.offset(0,1));
        srcRange.clearContent();
      }
      else {
        range.clearContent();
        return;
      }
    }

Modified Script修改后的脚本

This needs to be installed as an onEdit trigger on sheet 2.这需要安装为工作表 2 上的 onEdit 触发器。

  • If user enters number in col 1 or 2 then it gets the row from SS1.如果用户在 col 1 或 2 中输入数字,则它会从 SS1 获取行。
  • It then deletes that row from SS1.然后它从 SS1 中删除该行。
  • If user enters number 0 in col 1 or 2, then all rows are moved to SS3 from SS2.如果用户在 col 1 或 2 中输入数字 0,则所有行都从 SS2 移动到 SS3。
function updater(e) {
  // Get details about change in sheet
  let range = e.range;
  let sheet = range.getSheet();
  let value = e.value;
  let row = range.getRow()

  // check if it is request for information from SS1
  let wantInfo = (
    sheet.getSheetName() == "Foglio1" &&
    row != 1 &&
    (
      range.getColumn() == 1 ||
      range.getColumn() == 2
    )
  )

  // check if it is a 0, which means they are done
  let done = (value === "0")

  // If request for info
  if (wantInfo & !done) {
    // Get details of SS1
    let s1 = SpreadsheetApp.openById("1OfPEnvfA7PFLgzLTdfPycCPxsvavE93fHxQ1gFHaUt8")
    let s1sheet = s1.getSheetByName("Foglio1")
    let s1range = s1sheet.getDataRange()
    let s1values = s1range.getValues()

    // Prepare variables
    let output = [];
    let rowToDelete;

    // filter values in s1 to check if same as value searching
    newS1values = s1values.filter((row, index) => {
      if (row[0] == value){
        // if found, set row to delete
        rowToDelete = index + 1
        // remove first value from row
        row.shift()
        // add row to output array
        output.push(row)
        // don't include in newS1values
        return false

      } else return true // else do include in newS1values
    })

    // set range for new row in S2
    let outputRange = sheet.getRange(row, 3, 1, 8);
    // set values for range
    outputRange.setValues(output)
    // delete row in S1
    s1sheet.deleteRow(rowToDelete)

  } else if (done) {
    // if value entered is 0
    // get SS3 details
    let s3 = SpreadsheetApp.openById("1rfYBx3WFko3OmzbbtvlOdN1INkF0Uq6v2sM4d_sMejw")
    let s3sheet = s3.getSheetByName("Foglio1")
    
    // get all data from SS2
    let s2range = sheet.getDataRange();
    let s2values = s2range.getValues();
    // remove headers from value array
    let headers = s2values.shift();
    
    // remove last row (with 0)
    s2values.pop();

    // for each row in SS2 value array, add to SS3
    s2values.forEach(row => s3sheet.appendRow(row))

    // Delete all from SS2
    sheet.clearContents()
    sheet.clearFormats()

    // Add the header to SS2 again
    let headerRange = sheet.getRange(1,1,1,headers.length)
    headerRange.setValues([headers])
  }
}

function init(){}

Notes笔记

  • Ensure that the sheet names, ie "Folgio1" are correct.确保工作表名称,即“Folgio1”是正确的。
  • You will need to run the function init to grant permissions before it starts working.您将需要运行 function init以在它开始工作之前授予权限。

Reference参考

暂无
暂无

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

相关问题 从Google电子表格文档中获取工作表名称 - Get sheets names from google spreadsheets docs Google脚本可编译来自多个不同电子表格的数据,这些电子表格是同一文件的副本 - Google script to compile data from multiple different spreadsheets that are copies of the same file 我想使用谷歌应用程序脚本在新的谷歌工作表中对不同工作表(位于不同电子表格)中的值求和 - I want to sum the values present in different sheets(located in different spreadsheets) in a new google sheet using google app script Google Apps脚本,是从外部电子表格中检索数据的最快方法 - Google Apps Script, fastest way to retrieve data from external spreadsheets Google电子表格和表格 - Google Spreadsheets and sheets 在四张纸上隐藏除今天以外的所有列(Google Spreadsheets脚本) - Hide all but today's columns on four sheets (Google Spreadsheets script) 使用Ajax get从Google电子表格中检索数据 - Retrieve data from google spreadsheets using ajax get Google Spreadsheets:有条件,包括从不同电子表格导入的行 - Google Spreadsheets: conditional, includes rows imported from different spreadsheets 如何从 Google 表格中获取数据并在 Google Apps 脚本中处理它们? - How to get data from Google Sheets and handle them in Google Apps script? 将 Google 电子表格 API V4 (spreadsheets.values.get) 与 Private Sheets 连接起来 - Connect Google Spreadsheets API V4 (spreadsheets.values.get) with Private Sheets
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM