使用公式从另一个谷歌工作表导入数据并删除原始数据 [英] Importing data with formula from another google sheet and deleting original data

查看:11
本文介绍了使用公式从另一个谷歌工作表导入数据并删除原始数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下脚本从另一个 Google 表格导入数据,但它没有引入我需要的公式.

I am using the following script to import data from another Google Sheet but it doesn´t bring in the formula I need with it.

我已经尝试过 ImportRange 但这似乎只是同步了两个工作表,我需要删除原始工作表(工作表 1)中的数据,但这也会在我购买的工作表中删除它(主表).

I have tried ImportRange but that seems to just sync the two sheets and I need to delete the data in the original sheet (Sheet 1) but that also deletes it in the sheet I bought in to (Master Sheet).

还有其他方法可以做到这一点吗?

Is there another way to accomplish this?

function Copy() {
  var sss = SpreadsheetApp.openById('Sheet 1 ID');
  var ss = sss.getSheetByName('results');
  var range = ss.getRange('2:688');
  var data = range.getValues();
  var tss = SpreadsheetApp.openById('Master Sheet ID');
  var ts = tss.getSheetByName('results');
  ts.getRange(ts.getLastRow() + 1, 1, data.length, data[0].length).setValues(
    data
  );
  var sheet = SpreadsheetApp.openById('Sheet 1 ID').getSheetByName('Roger');
  ss.getRange('2:688').clearContent();
}

推荐答案

问题:

  • 不使用 getValues() 检索公式
  • Issue:

    • Formulas are not retrieved with getValues()
      • 使用 range#getFormulasrange#getValues 并混合这两个数组以创建一个公式和值数组,以便稍后 setValues().
      • 使用高级 Google 服务访问 Sheets API 以直接获得组合.
      • Use range#getFormulas and range#getValues and mix those two arrays to create a array of formulas and values to setValues() later.
      • Use Advanced Google Services to access Sheets API to directly get the mix.
        var data = range.getValues();
        data = range.getFormulas().map(function(e, i) {//i=index of row(e)
          return e.map(function(f, j) {//j = index of column(f)
            return f === "" ? data[i][j] : f;
          });
        });
      

        var req = {
          ranges: 'results!2:688',
          valueRenderOption: 'FORMULA', //get formulas with values
        };
        var r = Sheets.Spreadsheets.Values.batchGet('Sheet 1 ID', req);
        data = r.valueRanges[0].values;
      

      参考:

      • getFormulas
      • 启用高级服务
      • batchGet
      • 这篇关于使用公式从另一个谷歌工作表导入数据并删除原始数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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